Running a command line through shell script is not working [duplicate] - shell

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 4 years ago.
I have an application in Unix. I use the below command to connect to it:
./application -a "connect"
I want to do the same through the shell script, for which i assigned the command line to a variable like:
newcommand = './application -a "connect"'
$newcommand
But this is not working.
However the first part of the code is working. i.e.,:
newcommand = "./application"
$newcommand
Can anyone point out what i am missing.

Believe it or not, this:
newcommand = "./application"
...has the shell run the command, newcommand with the arguments, =, and ./application.
In shell simple assignments cannot have any unprotected whitespace or they'll be interpreted as a command.
Consider:
newcommand=./application
$newcommand
...notice that there's no space around the = sign in the assignment.

Related

How to add quoted parameters to a quoted command in a shell script? [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Closed 2 years ago.
I want to compose a command in a shell script like this:
#!/bin/sh
APPLICATION="date"
PARAMETER="-d '2020-01-01 1:23'"
CMD="${APPLICATION} ${PARAMETER}"
${CMD}
The 'PARAMETER' is supposed to hold parameters that need to be quoted themself. Unfortunately it does not work like this. Escaping them via PARAMETER="-d \"2020-01-01 1:23\"" also does not work.
After you've build CMD up, it is just string. It contains what can be interpreted by you as a command, but the shell sees it as a bare string.
If you want the string to reinterpret it, you need to eval it:
eval "$CMD"
However, eval is often considered evil.

Find out what the (shell) script was invoked with [duplicate]

This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.

reading a line from file using shell script [duplicate]

This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it
IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT
But this script echo empty string. Where I am making mistake?
If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.
Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript
As an alternative that's both efficient and compatible with POSIX sh:
IFS= read -r SERVER_IP_PORT <"$IPAddressFile"

Is it mandatory to use #!/bin/bash even inside bash subshell [duplicate]

This question already has answers here:
Writing a Bash script without the shebang line
(2 answers)
Bash script execution with and without shebang in Linux and BSD
(2 answers)
Closed 5 years ago.
If after logging into my system I type: bash (to use bash subshell) and then try to run a bash script (e.g. example.sh), then does it matter if I do not put #!/bin/bash as the first line of the script or it is fine since I am already inside bash subshell?

Bash variable assignment and command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I have a shell script that will let me access global variables inside the script, but when I try to create my own, it responds with: command not found.
#!/bin/bash
J = 4
FACE_NAME = "eig$J.face"
USER_DB_NAME = "base$J.user"
When I run the above script I get:
./test1.sh line 2: J: command not found
./test1.sh line 3: FACE_NAME: command not found
./test1.sh line 4: USER_DB_NAME: command not found
Any ideas?? I'm using Cygwin under Windows XP.
Try this (notice I have removed the spaces from either side of the =):
#!/bin/bash
J="4"
FACE_NAME="eig$J.face"
USER_DB_NAME="base$J.user"
Bash doesn't like spaces when you declare variables - also it is best to make every value quoted (but this isn't as essential).
It's a good idea to use braces to separate the variable name when you are embedding a variable in other text:
#!/bin/bash
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"
The dot does the job here for you but if there was some other character there, it might be interpreted as part of the variable name.
dont' leave spaces between "="
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"

Resources