Run script with sh or bash [duplicate] - bash

This question already has answers here:
Bash script process substitution Syntax error: "(" unexpected
(3 answers)
shebang not working to run bash scripts in linux
(1 answer)
Syntax error: "(" unexpected -- with !(*.sh) in bash script [duplicate]
(3 answers)
syntax error near unexpected token `<'
(2 answers)
Closed 3 years ago.
I have a .sh file. It includes bash syntax.
#!/bin/bash
function foo() {
// do something
}
doo()
// do something
sh doesn't link to bash on my system.
Below command doesn't work:
sh sample.sh
It throws syntax error. Below command works fine.
bash sample.sh
I think '#!/bin/bash' is useless for my case. I know that sh != bash. But do I must specify sh/bash/etc like upper example to run .sh file?

The shebang (#!) is only used if you execute the file (i.e: ./sample.sh). Of course, the file must have execution permissions.
If you execute sh with your file as argument its content is interpreted by sh (not taking into account the shebang). The same for bash or any other command that reads the file represented by the first argument.

Related

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"

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

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.

The function definitions in shell use dash [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 6 years ago.
The following code:
#!/bin/bash
function me-test()
{
echo 'test'
}
me-test
The execution method below is not correct:
#sh 1.sh
1.sh: line 6: `me-test': not a valid identifier
but the execution method below is correct:
#./1.sh
test
In other programming languages,it can not using dash to define function.For example,python.
why shell is so that?
sh 1.sh runs the script as a Bourne Shell script and ./1.sh runs it as a bash script because of your line #!/bin/bash (this is called a shebang which tells the shell which interpreter to use) calls the bash interpreter to run the script. bash allows for hyphens in function names but Bourne Shell does not. The two are very similar but different programming languages.
If you changed #!/bin/bash to #!/bin/sh you'd get an error every time you ran the program.

`Syntax error: "(" unexpected` when trying to set a list of files as an array variable in bash [duplicate]

This question already has an answer here:
I got 'syntax error: "(" unexpected' (expecting "done") [duplicate]
(1 answer)
Closed 7 years ago.
Maybe I'm being really silly here but I can't seem to figure this out:
#!/usr/bin/env bash
...
DELTAS=($(ls -p /foo/bar/ | grep -P '^\d+[^\.]+\.sh$'))
...
If I run this interactively it's fine, and echo $DELTAS returns:
1-foo.sh
2-bar.sh
However, when this is run inside a bash script, I receive Syntax error: "(" unexpected; any ideas?
I'm guessing that you've not put #!/bin/bash at the top of your script, and so it's running using /bin/sh rather than /bin/bash. This would run under dash rather than bash on Ubuntu, for example.
That syntax is a bash extension.

I got 'syntax error: "(" unexpected' (expecting "done") [duplicate]

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 7 years ago.
I have a very simple shell script which I'm using to loop through directories, and call another shell script. I wrote it on my local machine (OS X running Bash 3.2) and am using it on a remote server running Bash 4.2.
On the server, when I type which bash, I get /bin/bash, so I added the line on top. I still get this error, pointing to the line that begins arrIN=...
8: run_all_verification.sh: Syntax error: "(" unexpected (expecting "done")
The shell script:
#!/usr/bin/bash
# Base name for all experiments
BASE_EXP_ID=$1;
for i in ${BASE_EXP_ID}*
do
# Split file name by "__"
arrIN=(${i//__/ });
EXP_ID=${arrIN[0]}
NUM_FEATURES=${arrIN[1]}
echo "${EXP_ID} ${NUM_FEATURES}"
sh run_verification.sh ${EXP_ID} ${NUM_FEATURES}
done
Your error message is from Dash, probably because you ran sh filename.
To run a script with Bash, use bash filename (or ./filename).

Resources