Shell script: unexpected `(' [duplicate] - shell

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.

Related

Cron doesn't accept bash syntax

I have a bashscript that I'm running with crontab. Unfortunately, a script that works fine when run manually fails with the error:
Syntax error: "(" unexpected (expecting "}")
Where the line in question is line 22 which is:
declare -a PREV_TOTAL=( $(for i in ${range[#]}; do echo 0; done) )
In the larger context:
TOTAL_CPU_USAGE=0
TOTAL_CPU=$(grep -c ^processor /proc/cpuinfo) #set number of CPUs to check for
declare -a 'range=({'"0..$TOTAL_CPU"'})'
let "TOTAL_CPU=$TOTAL_CPU - 1"
#declare array of size TOTAL_CPU to store values (eg. 8 cpus makes arrays of size 8)
declare -a PREV_TOTAL=( $(for i in ${range[#]}; do echo 0; done) )
declare -a PREV_IDLE=( $(for i in ${range[#]}; do echo 0; done) )
This works when manually just fine, but I don't understand what I'm doing wrong that causes cron to give this error? If you know I'd be very appreciative. Thanks.
EDIT: My crontab looks like this:
# m h dom mon dow command
SHELL=/bin/bash
#reboot cd /home/ubuntu/waste-cloud-computing/probe && probe.sh >> /var/log/somelogfile.log 2>&1
And I access it with sudo crontab -e. I'm still getting the issue while providing the SHELL variable.
EDIT 1: Thanks to some help I got past the syntax issues by ensuring the shell was using bash. Now I get the error, /bin/bash: probe.bash: command not found. I assume its some kind of PATH issue, but which bash returns /bin/bash so it seems normal to me. Maybe someone knows what's up?
cron jobs are run by sh by default, not bash. If you are using ubuntu/vixiecron, you can set the SHELL env variable at the top of the crontab to make cron run the commands in your crontab with bash.
SHELL=/bin/bash
If the script you want to be run is a bash script, make sure you have a shebang at the first line:
#!/bin/bash
Also note that there will be other potential troubleshooting steps if your scripts depend on a particular user's profile, env vars, etc. depending on which crontab you are editing.
Thanks to the help of the people here I found my issue was not syntax but rather the use of sh over bash. This was fixed by setting the crontab this way so future users can see:
# m h dom mon dow command
SHELL=/bin/bash
#reboot cd /home/ubuntu/waste-cloud-computing/probe && ./probe.sh >> /var/log/somelogfile.log 2>&1
The key points are the SHELL variable being set and the ./ before running the script.

Dash -x fails with Bad substitution error

I'm trying to learn how to write portable shell scripts, to do so I'm starting to migrating my personal utilities from bash to sh (dash on my system). There is however a error I'm getting in all cases when I try to run the scripts in debugging mode $ dash -x script
For instance, on this script:
#!/bin/sh
echo hi
If I run it as: $ dash script, I get the 'hi' string, however if I run it as: $ dash -x script or if I add the set -x command before echo:
#!/bin/sh
set -x
echo hi
It fails with the error:
script.sh: 3: script.sh: Bad substitution
this makes very difficult to debug my scripts. I'm running ubuntu 12.04 with dash 0.5.7-2ubuntu2
Just by the time I finished writing my question I realized I was using a personalized PS4 (which is used in xtrace mode), my PS4 was defined as:
>>(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }
I changed temporarily as PS4=">>" and everything went ok, I can now debug my scripts on dash. Hope this helps someone.

sudo bashfile makes syntax error

i have got a bash file which i want to toggle via the sudo crontab list. Problem is, that it does not work, because when i run the script with sudo, there is a syntax error message on this line:
size=(`du -h $backupDir --summarize`)
If i run the same script without, i have to type the sudo pw, but it works without any problems.
I allready tried a few variations with brackets, with or without backticks, with or without spaces, etc but nothing helped. The error message is:
Syntax error: "(" unexpected (expecting ";;")
Any help?
The problem here is that you use bash-syntax, and the script (when it is executed from cron) is interpreted by /bin/sh (that known nothing about arrays and the () construction.).
You must either specify bash as an interpreter of the script using she-bang notation:
#!/bin/bash
or run the script explicitly with bash from cron:
bash /path/to/script
or rewrite script so, that it could run without arrays.
Fro readability use the $() form
size=$(du -h $backupDir --summarize)
If you want to stick to back quotes, then
size=`du -h $backupDir --summarize`

Input to Expect Script

in my test file, I had this line hello$LG. So, if I do
`cat /test`
It will show in my bash shell - hello$LG
But when I try to do this in my expect script,
$> expect test.sh `cat /test`
It gives me this {hello$LG} . I don't know why it is getting {}. Anyone knows how to fix this?
Expect is based upon TCL, you should use square brackets to execute code. When trying to execute a shell command you should use either exec or open or spawn.
I am not sure what the test.sh does, but
expect [exec test.sh 'cat /test']
might just do the trick.
There's more on running other programs from Tcl here:
http://www.tcl.tk/man/tcl/tutorial/Tcl26.html

source .bashrc from .cshrc

Is it possible to source a .bshrc file from .cshrc in a non-interactive session?
I'm asking because tcsh is our default shell at work and the .cshrc has to be used to set up the environment initially.
However, I am not really familiar with the tcsh and I have my own set-up in bash, so right now I have the following lines at the end of my .cshrc file:
if ( $?prompt && -x /bin/bash) then
exec /bin/bash
endif
This works fine, loading my environment from .bashrc and giving me a bash prompt for interactive sessions but now I also need the same set-up for non-interactive sessions, e.g. to run a command remotely via SSH with all the correct PATHs etc.
I can't use 'exec' in that case but I can't figure out how to switch to bash and load the bash config files "non-interactively".
All our machines share the same home directory, so any changes to my local *rc files will affect the remote machiens as well.
Any ideas welcome - thank you for your help!
After some more research I'm now quite sure that this won't work, but of course feel free to prove me wrong!
To load the environment in bash I have to switch to a bash shell. Even if that is possible "in the background", i.e. without getting a prompt, it would still break any tcsh commands which would then be attempted to execute under bash.
Hmmmm, back to the drawing board...
If $command is set there are arguments to csh, so it is a remote shell command. This works for me in .cshrc:
if ($?command) then
echo Executing non-interactive command in bash: $command $*
exec /bin/bash -c "${command} $*"
endif
echo Interactive bash shell
exec bash -l
Test:
$ ssh remotehost set | grep BASH
BASH=/bin/bash
...
proves that it ran in Bash.

Resources