read more than one parameters right after the command in bash - linux

I am making a bash script and I want it to be just one line, meaning it will not have any interaction with the users and the parameters will be on the same line as the command. Once the user clicks return, it will output the result.
Right now, I have something that looks like this:
#! \bin\bash
read $1 $2
do something with $1 and $2
However, if I name my script "test" when I type in test at the beginning of the command line, I will have to type enter for the rest of the script to be executed. How should I modify it so that I can run the entire thing on just one line?

The standard way to pass parameters to a script is not with read (which actively waits for input from stdin), but just to call your script with the parameters on the same line:
./my_script.sh param1 param2
Then inside the script, you can access these parameters using $1, $2, etc. Example (note also the first line - this describes what shell should be used to run the script, and should be a valid path - ie /bin/bash, not backslashes):
#!/bin/bash
echo "First: $1 Second: $2"
Then call the script:
$ ./my_script.sh Hello There
First: Hello Second: There

What you probably need is this :
You script name test.sh contains the following:
#!/bin/bash
echo "$1 $2"
Then, change permission so that you can execute the script on the command line :
chmod u+x test.sh
and run the script with arguments (two in this case) :
./test.sh tik tak
will return
tik tak

Related

not able to use local variable outside the function after exporting

This is my script , even after using the export command not able to use variable outside of the block. Below is the code that i have tried. I also tried other option like declare -x var, but that is also not working.
Can someone please please comment on this , am i doing right ?
#!/bin/bash
{
var="123"
export var # exporting the variable so that i can access from anywhere
echo "var is "$var # able to get the value of this variable
} | tee log.txt
echo "var is "$var # not able to get the value of this variable
Because the pipe is causing the code between the braces to execute in a sub-shell you need to find a way to capture that data as opposed to storing it in a variable that is not accessible from the rest of the code. An example would be to store the output of a function in a variable, or to access it via command substitution. If you have script.sh as such:
#!/bin/bash
function get_pizza() {
echo "Pizza"
}
myvar=$(get_pizza)
printf "myvar is '%s'\n" $myvar
echo "Plain echo follows:"
echo $(get_pizza)
and then run bash script.sh you will get output as such:
[user#host]$ bash ./script.sh
myvar is 'Pizza'
Plain echo follows:
Pizza
Then if you still want to write to a file via tee, you can pipe your whole script to tee:
bash ./script.sh | tee foo.log
If you only want parts of the script to goto a file, you'll can also handle that with I/O redirection within the script: echo pizza > foo.log

Calling shell script within a script

Below are my two script codes:
**a.sh**
#!/bin/sh
SRC_PATH="/xx/xxxx"
HOST='ftp.xxx.xxx.com'
USER='xxxx'
PASS='xxx'
FTP_SRC_PATH='out'
**b.sh**
#!/bin/sh
/xx/xxx/a.sh
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASS
binary
prompt off
cd $FTP_SRC_PATH
lcd $SRC_PATH
mget IMS_*.ZIP
bye
END_SCRIPT
My issue is when I am running b.sh , it is not calling a.sh and using the variables defined in it to connect to ftp server.
I have seen many solutions already online but things doesn't seem to work.
Please help
You can include a.shwith the dot command:
. /xx/xxx/a.sh
You can call the script a from within b as follows:
sh ./a.sh
Make sure both the shell scripts are in same file path. Also, you can pass the value within scripts as follows:
sh ./a.sh $d $e
The values passed here can be accessed as $1 $2.
Here is an example:
a.sh:
!/bin/bash
yourvalue="test"
sh ./b.sh $yourvalue
and I'm able to access the variable passed here in the shell script b as $1.

Positional parameters for shell not working as expected

I am trying to learn bash commands, and some very basic commands are not working as I expect...http://www.tutorialspoint.com/unix/unix-special-variables.htm
http://i.stack.imgur.com/F5VGK.png
Script:
#!/bin/bash
name="john"
other="shawn"
echo $name
echo $other
echo $1
echo $2
echo $#
echo $#
Output:
$ new
john
shawn
0
$
$1, $2, etc and $# have special meaning in bash scripts. They refer to the arguments passed to the bash script, so if you have a script in a file called foo.sh like:
#!/bin/bash
echo "Number of arguments: $#";
echo "First argument: $1";
echo "Second argument: $2";
If you chmod +x foo.sh and then run:
./foo.sh first second
You will see:
Number of arguments: 2
First argument: first
Second argument: second
$1 refers to the first command line argument passed to the script. The script is foo.sh, so anything after the script name will become a command line argument.
The default command line argument separator is the "space", so when you type ./foo.sh first second, bash stores first into $1 and second into $2.
If you typed:
./foo.sh first second third FOURTH fifth
bash would store third in the variable $3, FOURTH in the variable $4, and so on.
Is your script named 'new' ? In that case run it as follows one by one and you will get an idea how this works:
./new
./new a
./new a b
when you ran your script you did not pass any arguments. the number of arguments passed to the scripts are show by echoing "echo $#". and your output clearly shows that the "echo $#" command returned "0" count. pass the argument when you call your script like below
./new argument1 argument2

file as input for script when executing the script

I want to give a file as input while executing the script
For example i have the code:
while read line
do
done<xyz
while executing the script
$sh file.sh input.txt
the file input.txt should go as input in the place of xyz for the while loop.
Use $1 to get the parameter:
while read line
do
...
done < "$1"
$1 will contain the first parameter you give to the script.
Example
$ cat a
#!/bin/bash
echo "I have been given this parameter --> $1"
$ ./a hello
I have been given this parameter --> hello

Special symbols in Linux

what is the purpose of $1? while executing java file, we will give it as command line argument. To which it will refer?
You gave a little context here, but I thinks it's shell argument.
ARGUMENTS
If arguments remain after option processing, and neither the -c nor the -s option has been supplied, the first argument is assumed to be the name of a file containing shell commands. If bash is invoked in this fashion, $0 is set to the name of the file, and the positional parameters are set to the remaining arguments. Bash reads and executes commands from this file, then exits. Bash's exit status is the exit status of the last command executed in the script. If no commands are executed, the exit status is 0. An attempt is first made to open the file in the current directory, and, if no file is found, then the shell searches the directories in PATH for the script.
For more detailed, try man bash.
EXAMPLE
$ cat ./test.sh
#!/bin/bash
echo $0
echo $#
$ ./test.sh hello world
./test.sh
hello world

Resources