Can someone explain to me why the following shell script line throws this error:
Syntax error: "(" unexpected
Shell scripts expect variables to be set in the pattern of:
VARIABLE=value
You can't have any additional = signs in there. However, you can execute other scripts like this:
VARIABLE=$(basename $1)
VARIABLE=`basename $1`
Either one works.
In your case, I can't tell what you're doing, but it isn't right at all. My guess is that you need to do this:
env LD_LIBRARY_PATH=$(basename $1)
Related
I want to run this script:
#!/bin/bash
echo <(true)
I run it as:
sh file.sh
And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this.
I'm a beginner at shell scripting , but as I understand:
the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?
Maybe it's something simple, but I couldn't find an explanation or solution.
You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.
See also: role of shebang at unix.SE
Remove export POSIXLY_CORRECT=1 from your ~/.bashrc or ~/.profile (etc.) files.
The issue is that process substitution is an added bash feature that is not part of the posix standards.
sh file.sh
errorsh: 3: Syntax error: "(" unexpected
solution:
bash file.sh
When i am defining array with the required values it's throwing below error
db_backup_daily.sh: 103: /home/user/Desktop/db_backup_daily.sh: Syntax error: "(" unexpected (expecting "fi")
The code which I have provided is getting executed in online shell editor but the same thing when I am trying to execute from my terminal it throwing error
#!/bin/sh
year=$(date +"%Y")
month=$(date +"%B")
day=$(date +"%d")
db_backup=/home/user/Documents/db_backup
YEAR_DIR=/home/user/Documents/db_backup/$year
MONTH_DIR=/home/user/Documents/db_backup/$year/$month
DAY_DIR=/home/user/Documents/db_backup/$year/$month/$day
FILE_NAME_ARRAY=( USER_TABLE_FILE PMT_TABLE_FILE FBA_PO_TABLE_FILE VENDOR_SUPPLIER_TABLE_FILE )
LOG_FILE_NAME_ARRAY=(USER_TABLE_LOG_FILE PMT_TABLE_LOG_FILE FBA_PO_TABLE_LOG_FILE VENDOR_SUPPLIER_TABLE_LOG_FILE)
TABLES_NEED_To_BACKUP=(Add_task checklist_task_management)
echo 'code executed successfully'
above code should get executed from the terminal
#!/bin/sh means this script is meant to be run on the system's default POSIX shell, which is not always a link to bash/ksh, thus not guaranteed to support arrays. You need to change it to:
#!/bin/bash
Same problem as this OP, but must be a seperate cause.
The following script:
#!/bin/sh
arr=("cat" "dog" "bird")
Works interactively (debian) but fails when called by crontab with:
/bin/sh: 2: /path/zero_check.sh: Syntax error: "(" unexpected
I've tried with #!/bin/bash shebang, and declaring array with declare -a arr=("cat" "dog" "bird"), to no effect.
Any idea why?
The problem here is that you are using this shebang:
#!/bin/sh
Whereas arrays are something Bash specific that shell does not allow.
So to make it work, change the shebang of your script to Bash:
#!/bin/bash
Specify your interpreter explicitly in the crontab entry. Use
bash /path/zero_check.sh
rather than
/path/zero_check.sh
Just for the documentation,
i had an old script to run which had an Syntax Error in the Shebang:
#/bin/bash
instead of
#!/bin/bash
Also check the Script is executable of course.
Very similar problem with incorrect bash function declarations. This works OK from the command line, but it causes cron to fail...
function test () { ... }
Cron should save the errors in /var/mail
I also recommend linting with "shellcheck" because it found another error I didn't notice.
I have this command in a unix Bash Shell Script:
midori http://www.test.com/test.php?id=$i&test=yes#test(test) &
But I get:
syntax error near unexpected token `('
How do I use this?
As commented, this should make it:
midori "http://www.test.com/test.php?id=$i&test=yes#test(test)" &
Note that giving the parameter within double quotes makes midori understand it as a string. As you were giving it without any quote, bash was trying to interpret it and getting puzzled with the ( in (test).
Today I tried this and was somewhat surprised to find that it didn't work:
$ nice -n 10 { ./configure && make ; }
-bash: syntax error near unexpected token `}'
Is there a way to use grouping in a "subcommand"?
Have you tried this ? =)
nice -n10 bash -c './configure && make'
You can't just pass shell syntax to the argv of a program and expect it to understand it. Specifically, the error you're seeing is because of the && and ;, which are "list operators" which separate commands. Bash is trying to evaluate the arguments to nice as:
nice '-n' '10' '{' './configure'
Bash then tries to evaluate the next command after the && (make), then the next command, which is }. Technically, braces are both "reserved words" and "control operators". Different shells treat bare braces a bit differently but that's an esoteric detail. The point is depending on the shell that will either be a parsing error (like here), or an error due to not being able to find a command named "}" (usually the former.
Exceptions to this rule exist only within the shell itself. For instance, the Bash coproc keyword works like this, enabling special parsing and evaluation of its arguments almost exactly as in your example.