Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top