Filename substitution gives syntax error in script , works in console - bash

I have several files under /var/log/uwsgi/ named similar to domain.123456789
I'm trying to loop over the files as given below
for FILE in /var/log/uwsgi/domain.+([[:digit:]]); do
gzip $FILE;
done
this works in console. but when run as a part of a script, i get the following syntax error.
script.sh: line 16: syntax error near unexpected token `('
how can i use substitution in shell scripts?

Make sure that the extglob shell option is enabled in your script by adding:
shopt -s extglob
Without this, the shell won't recognise your +([[:digit:]]) pattern and you will get an error.
You probably already have this set in your bash profile which is why it works in the console.

Related

syntax error near unexpected token `(' bash [duplicate]

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 error in shell script

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

Error 'Syntax error: "(" unexpected' when declaring arrays in 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.

bash function syntax error in cygwin

I'm using cygwin. I made a sh file like the following
#!/bin/sh
function bash {
local var="local variable"
echo $var
}
then I execute this file
./test.sh
The result returned is
./test.sh
./test.sh: line 2: syntax error near unexpected token `$'\r''
'/test.sh: line 2: `function bash {
I have no clue how to fix it and use the capability of writing function in bash scripts. Thank you in advance!
Regards,
The error message is trying to tell you there are CRLF line endings, and it doesn't like the CR ($'\r' being a bash way of representing CR, carriage return).
Using Cygwin, you need to do this before executing any bash file:
sed -i 's/\r$//' name_of_your_script.sh
Once done, you can use it normally. If you make any change in the code, use that line again.
This is because there is a problem with the CR when using bash files in Cygwin. This line eliminates those bothering CR and solves the problem.

Parentheses don't work in Bash script

I'm writing a script that needs to erase everything from a directory except two directories, mysql and temp.
I asked a question earlier and got this code that works in the command line:
rm -rf !(mysql|temp)
However it doesn't work in the script. I get this error "Syntax error: "(" unexpected".
Is there something special about parentheses I need to do?
You probably need to explicitly enable extended patterns in your script:
shopt -s extglob

Resources