This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]
Related
This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
a.sh content
#!/bin/bash
awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 5;grep 'cpu ' /proc/stat)
It works when pasted to shell directly,but not work when executed as a script file sh a.sh.
what is the difference ?
This most likely mean your default shell is not "/bin/bash". You can see which shell is your default shell with this command:
echo $SHELL
If you never changed it, chances are that it is "/bin/sh".
Since it works in your shell, the easiest is to change your shebang line at the top so that it reference the path of your default shell instead of "/bin/bash".
But if you end up wanting to make it work in bash and change your default shell to Bash, this is how you do it:
chsh -s /bin/bash
This question already has answers here:
What's the point of eval/bash -c as opposed to just evaluating a variable?
(3 answers)
The 'eval' command in Bash and its typical uses
(11 answers)
Closed 2 years ago.
There is something i do not understand with strings in bash:
Look at this script:
#!/bin/bash
tmp="ls"
"$tmp"
This script executes ls command and display result in the console.
Now look at this script:
#!/bin/bash
tmp="ls > out.txt"
"$tmp"
This second script does not execute ls and displays this error:
line 3: ls > out.txt: command not found
I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.
Thanks
This question already has answers here:
Syntax error in shell script with process substitution
(4 answers)
Closed 3 years ago.
I wonder why it doesn't work.
Please advise me.
1. working
$ nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
$ echo $nu
1291.5
2. not working
$ cat test.sh
#!/bin/bash
nu=`awk '/^Mem/ {printf($2*0.7);}' <(free -m)`
echo $nu
$ sh test.sh
test.sh: command substitution: line 2: syntax error near unexpected token `('
test.sh: command substitution: line 2: `awk '/^Mem/ {printf($2*0.7);}' <(free -m)'
Could you please try following.
nu=$(free -m | awk '/^Mem/ {print $2*0.7}')
echo "$nu"
Things taken care are:
Use of backtick is depreciated so use $ to store variable's value.
Also first run free command pass its standard output as standard input to awk command by using |(which should be ideal way of sending output of a command to awk in this scenario specially) and save its output to a variable named nu.
Now finally print variable nu by echo.
Since <(...) process substitution is supported by bash not by sh so I am trying to give a solution where it could support without process substitution (which I mentioned a bit earlier too).
The <( ) construct ("process substitution") is not available in all shells, or even in bash when it's invoked with the name "sh". When you run the script with sh test.sh, that overrides the shebang (which specifies bash), so that feature is not available. You need to either run the script explicitly with bash, or (better) just run it as ./test.sh and let the shebang line do its job.
The reason to add a shebang in a script is to define an interpreter directive if the file has execution permission.
Then, you should invoke it by, for example
$ ./test.sh
once you have set the permission
$ chmod +x test.sh
This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 8 years ago.
I have many BASH scripts called in sequence, e.g., script1.sh contains:
#!/bin/bash
bash script2.sh
bash script3.sh
bash script4.sh
script2.sh contains:
#!/bin/bash
file_a="1.txt"
cp $file_a /tmp/$file_a.tmp
script3.sh contains:
#!/bin/bash
wc -l /tmp/$file_a.tmp
script4.sh contains:
#!/bin/bash
cat /tmp/2.txt $file_a.tmp > file3.txt
Each file requires access to a small collection of variables. How can I pass the variables from one script onto the next?
You have many options.
The first method would be making the variable as the environment variable and pass to the script before the second script get executed.
The second method would be making the second script to run in the same shell.
The methods are described well here with the examples.
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 9 years ago.
So I have one bash script which calls another bash script.
The second script is in a different folder.
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
In this second script it has the line src=$(pwd) and since I'm calling that script from another script in a different directory, the $(pwd) returns the current directory of the first script.
Is there any way to get the current directory of the second script using a simple command within that script without having to pass a parameter?
Thanks.
I believe you are looking for ${BASH_SOURCE[0]}, readlinkand dirname (though you can use bash string substitution to avoid dirname)
[jaypal:~/Temp] cat b.sh
#!/bin/bash
./tp/a.sh
[jaypal:~/Temp] pwd
/Volumes/Data/jaypalsingh/Temp
[jaypal:~/Temp] cat tp/a.sh
#!/bin/bash
src=$(pwd)
src2=$( dirname $( readlink -f ${BASH_SOURCE[0]} ) )
echo "$src"
echo "$src2"
[jaypal:~/Temp] ./b.sh
/Volumes/Data/jaypalsingh/Temp
/Volumes/Data/jaypalsingh/Temp/tp/
Please try this to see if it helps
loc=`dirname $BASH_SOURCE`