I am learning how shell scripting in Unix and have written a simple shell script:
#!/bin/sh
NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"
However, whenever I run this in ubuntu as ./arrayScript.sh I am met with this error:
./arrayScript.sh: 3: NAME[0]=Zara: not found
./arrayScript.sh: 4: NAME[1]=Qadir: not found
./arrayScript.sh: 5: NAME[2]=Mahnaz: not found
./arrayScript.sh: 6: NAME[3]=Ayan: not found
./arrayScript.sh: 7: NAME[4]=Daisy: not found
./arrayScript.sh: 8: Bad substitution
I have come to understand that .sh files do not support arrays and that if I run the file as:
bash ./arrayScript.sh
It works fines, but I thought that running ./arrayScript.sh should have worked because that is what is given in the tutorial example? Can someone inform me whether I am doing something wrong, as I have copied the example letter for letter from here:
https://www.tutorialspoint.com/unix/unix-using-arrays.htm
#!/bin/sh is not bash. When you call it on cli with bash ./arrayScript.sh, the interpretor mentioned in the shebang is ignored and it will be parsed by bash. Your code works. When you simply call it with ./arrayScript.sh it uses that shebang to determine the interpretor. You have sh and not bash, so it picks the wrong interpretor for the code in the file.
Replace #!/bin/sh with #!/bin/bash and you should be fine.
Related
I have a very basic shell script containing
#!/bin/sh
NAME[0]="Hello"
echo ${NAME[0]}
So when I run this script an error occurs stating
./test.sh: 2: ./test.sh: NAME[0]=Hello: not found
./test.sh: 3: ./test.sh: Bad substitution
So basically I looked through a few tutorials and found this to be the basic way to declare arrays. So I am confused as to why this is an error. Any ideas?
You are starting your script as #!/bin/sh, which has a soft link to dash (The current version of sh which conforms with the POSIX 1003.2 and 1003.2a specifications for the shell) and dash doesn't support arrays. In debian 8 onwards dash has become default shell, so if you run ls -la /bin/sh the output will be /bin/sh -> dash
However bash still remains the default login shell, only the default /bin/sh used in shell scripts has been changed. So if you run your code on terminal, it will work just fine. More information on why this switch was made in Ubuntu can be found here.
If you want to use arrays in your script then you must start your script with #!/bin/bash
So your script works perfectly if modified like this
#!/bin/bash
NAME[0]="Hello"
echo ${NAME[0]}
More information on Dash as Sh DashAsBinSh
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).
I want use an array in my shell script test.sh as follows:
#!/bin/bash
index=100345
NAME[0]="Zara"
NAME[$index]="Qadir"
echo "First Index:" ${NAME[0]}
echo "Second Index:" ${NAME[$index]}
but when execute it by sh test.sh:
NAME[0]=Zara: not found
NAME[100345]=Qadir: not found
test.sh: ${NAME[...}: Bad substitution
How to solve it?
Don't do this:
sh test.sh
That runs the script under whatever is the default shell.
Instead, do this:
bash test.sh
This assures that it is bash which runs the script.
Example
Using your test.sh script, this generates errors:
$ sh test.sh
test.sh: 3: test.sh: NAME[0]=Zara: not found
test.sh: 4: test.sh: NAME[100345]=Qadir: not found
test.sh: 5: test.sh: Bad substitution
This does not:
$ bash test.sh
First Index: Zara
Second Index: Qadir
On my system, sh is a link to dash which is a fast POSIX shell but it does not support arrays. To get fancy features like arrays, one must use a fancy shell like bash.
The array is not supported by the simple terminal. Use the command bash your_Script_Name.sh. EX: I have a script file array.sh in which I am using the array. In this case, i will run this script as:
bash array.sh
So I am trying to write my first shell script that can automatically run some C codes for me. I read some materials online and here is my short shell script:
#!/bin/sh
# script for grading assignment 3
echo -n "Enter the student's index >"
read index
echo "You entered: $index"
#### Functions
function question_one
{
gcc -pthread -o $index.1 $index.1.c
taskset -c 1 ./$index.1 5 5
}
#### Main
$(question_one)
As you can see, the shell script is quite simple and what it does is also quite easy to understand. First compile a C source file named like 1.1.c, 2.1.c or 3.1.c and then run the output file with just one single CPU.
When I run this script, looks like it can successfully compile the file but unable to run the output file correctly. The error message is "assignment_three_grading: line 18: Thread: command not found". However, if I type in the commands manually by myself, everything is fine.
$(question_one)
Change this to simply:
question_one
To invoke a function you just name it as if it were a regular command. Using $(...) captures its output and tries to execute that output as another command name: definitely not what you want here.
I have written the following code:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
And I am getting error:
array.sh: 3: array.sh: Syntax error: "(" unexpected
From what I came to know from Google, that this might be due to the fact that Ubuntu is now not taking "#!/bin/bash" by default... but then again I added the line but the error is still coming.
Also I have tried by executing bash array.sh but no luck! It prints blank.
My Ubuntu version is: Ubuntu 14.04
Given that script:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
and assuming:
It's in a file in your current directory named array.sh;
You've done chmod +x array.sh;
You have a sufficiently new version of bash installed in /bin/bash (you report that you have 4.3.8, which is certainly new enough); and
You execute it correctly
then that should work without any problem.
If you execute the script by typing
./array.sh
the system will pay attention to the #!/bin/bash line and execute the script using /bin/bash.
If you execute it by typing something like:
sh ./array.sh
then it will execute it using /bin/sh. On Ubuntu, /bin/sh is typically a symbolic link to /bin/dash, a Bourne-like shell that doesn't support arrays. That will give you exactly the error message that you report.
The shell used to execute a script is not affected by which shell you're currently using or by which shell is configured as your login shell in /etc/passwd or equivalent (unless you use the source or . command).
In your own answer, you say you fixed the problem by using chsh to change your default login shell to /bin/bash. That by itself should not have any effect. (And /bin/bash is the default login shell on Ubuntu anyway; had you changed it to something else previously?)
What must have happened is that you changed the command you use from sh ./array.sh to ./array.sh without realizing it.
Try running sh ./array.sh and see if you get the same error.
Instead of using sh to run the script,
try the following command:
bash ./array.sh
I solved the problem miraculously. In order to solve the issue, I found a link where it was described to be gone by using the following code. After executing them, the issue got resolved.
chsh -s /bin/bash adhikarisubir
grep ^adhikarisubir /etc/passwd
FYI, "adhikarisubir" is my username.
After executing these commands, bash array.sh produced the desired result.