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
Related
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.
Hi all. I gave a git-bash installed and want some automatisation. I've got a .bat file, which I want to run as
some.bat param | iconv -cp1251 > l.log | tail -f l.log
And I want to run it not in WinCMD but in git-bash shell - tell me plz how to do it?
Git bash on windows uses BASH. You can use bash to create a quick script that can take in parameters and echo them so that you can pipe them to your next utility.
It could look something like this
File: some.sh
#!/bin/bash
#Lots of fun bash scripting here...
echo $1
# $1 here is the first parameter sent to this script.
# $2 is the second... etc. $0 is the script name
then by setting some.sh as executable
$ chmod +x some.sh
You'll then be able to execute it in the git-bash shell
./some.sh param | cat ... etc | etc...
You can read about bash programming
I'd reccomend looking at some bash scripting tutorials such as this one
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
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.
I am having some trouble with a simple shell script on my ubuntu box. Here is the first part of the script:
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
servers=(
199.315.117.225
46.31.151.106
46.31.154.82
)
I run the script like this:
sudo sh script.sh
And this is the output:
script.sh: 6: script.sh: Syntax error: "(" unexpected
As far as I can tell, that is a valid array, so I don't understand why that paren is unexpected. I am a newbie so any help is much appreciated.
The problem is that you're running your script using a shell (namely Dash) that doesn't support a feature your script is using (namely Bash-style arrays).
The easiest fix is to change this:
#!/bin/sh
to this:
#!/bin/bash
so that your script is run using Bash instead of Dash.
/bin/bash is superset of /bin/sh... This array syntax works in bash.
https://superuser.com/questions/125728/what-is-the-difference-between-bash-and-sh
http://www.linuxjournal.com/content/bash-arrays
In shell script array should be mentioned by
bash:
declare -a varname
varname=( 192.168.1.1 192.168.1.2 192.168.1.2 )
and you can get all ip by
echo "${varname[#]}"
or
echo "${varname[*]}"
you can try the below code,
#!/bin/sh
LCL="192.168.1.1/24"
VPN="10.0.0.0/12"
local_interface="eth0"
virtual_interface="tun0"
ARRAY="199.315.117.225 46.31.151.106 46.31.154.82"
for i in $ARRAY
do
echo $i
done