0

Please find below script

if [ "$#" -eq 0 ]; then
  dbs=('test_db_1' 'test_db_2') 
  path='/var/backups/'
else
  dbs=( "$@" )
  path='/app/mybackups/'
fi
dbuser='test_user_name'
dbpassword='test_pass_word'
now="$(date +'%d-%m-%Y_%T')"
currentDate="$(date)"
dayToSubtract=365
cd ${path}
echo "location: $path"
for element in ${dbs[@]}
        do
                echo $element
                echo "Starting backup now for db: $element on $now"
                mongodump -u ${dbuser} -p ${dbpassword} --authenticationDatabase 'admin' -d $element --gzip --archive=${element}_$now.archive
                dateToBeRemoved=$(date --date="${currentDate} -${dayToSubtract} day" +%d-%m-%Y)
                echo $dateToBeRemoved
                fileToBeRemoved="${element}_${dateToBeRemoved}"
                echo Removing $fileToBeRemoved
                rm $fileToBeRemoved*
        done
echo All Done!
echo "location: $path"

I have been using this script for more than a year for backing up my dbs on the server on a daily basis and I also use it to take manual backups by passing command-line arguments to this script.

Recently I got a new server having ubuntu and its giving following error there

daily.sh: 2: daily.sh: Syntax error: "(" unexpected (expecting "fi")

Please find below screenshot for reference:

enter image description here

After adding:

#!/usr/bin/env bash

enter image description here

Getting same error:

enter image description here

Ran which bash, got the following

enter image description here

Added the shebang according to which bash result but still facing same issue enter image description here

When running from the current folder:

enter image description here

check for the BOM:

enter image description here

Ubuntu Info: Distributor ID: Ubuntu Description: Ubuntu 18.04.3 LTS Release: 18.04 Codename: bionic

2
  • 5
    You're using sh to run a bash script (that should be in the text, not in the picture), which points to dash in Ubuntu, not bash.
    – Quasímodo
    Commented May 19, 2020 at 10:45
  • 3
    Also, please don't attach screenshots of console output, but copy-and-paste it to the question body with appropriate formatting; in your case it is short enough that no highlighting should be necessary (and even then, there are other possibilities).
    – AdminBee
    Commented May 19, 2020 at 10:54

2 Answers 2

2

Add the shebang in the first line of your script :
(From the comment)

#!/usr/bin/env bash 

OR If you know the path.

#!/bin/bash

Or

#!/usr/bin/bash

wherever it is located.

14
  • 1
    #!/usr/bin/env bash uses the PATH to locate bash (or rather whatever interpreter you hand it). So if it's not installed you'll get the same you'd always get in such a case: /usr/bin/env: ‘bash’: No such file or directory ... because env helps to "delegate" to the interpreter it finds. If none is found, you get a meaningful error. Just as if you had given an absolute path to a non-existing bash binary: -bash: ./fle.sh: /bin/bash: bad interpreter: No such file or directory ... Commented May 19, 2020 at 11:02
  • @0xC0000022L : Thanks a lot for the info. Commented May 19, 2020 at 11:05
  • How would i find the location of this? /usr/bin/bash this does not exists for me and I tried adding both you suggested but both does not work
    – Abdul Moiz
    Commented May 19, 2020 at 11:08
  • @AbdulMoiz : Did you try this -> "#!/usr/bin/env bash" .. Commented May 19, 2020 at 11:10
  • When I check bash -n it does not give me any error.
    – Abdul Moiz
    Commented May 19, 2020 at 11:10
1

I am leaving the old answer below, as it adds context and details. But I think your misunderstanding stems from what you assume sh yourscript ought to do.

What it does is to use sh (which differs by system, as you noticed ...) as the interpreter for the script name you pass.

What you seem to intend, though, was

sh -c "your command ... whatever it is"

Now, why you insist on using sh as the interpreter when you've full well been told in comments and two answers that this is wrong, I don't know.

If your script is executable, has a proper hashbang and is either located in the PATH or invoked with its relative or absolute path, it will work.

But by insisting on using sh as the interpreter you are basically actively sabotaging all attempts to help you, because sh is not the same as sh -c COMMAND. And if your interpreter isn't Bash, using Bashisms will likely fail (as you witnessed).

And if you want this failure to be "more reliable" you can carve it in stone by using #!/bin/sh as the hashbang (instead of the suggested one), which will yield the same outcome as handing your script to sh on the command line.


You have several issues here. Your screenshot shows your script isn't executable (owned by root:root with file mode 0644). So when you are attempting to execute it with sh it will use whichever Shell sh happens to be on your system.

The reason this fails is threefold:

  • your script is not executable and not in the PATH
  • sh yourscript makes it - since it's not executable - use sh as the interpreter (which in case of Ubuntu is dash and doesn't understand all Bashisms)
  • your script is missing a hashbang, which provides the clue for the loader which interpreter to use for your "binary"

If, however, you'd add a hashbang such as:

#!/usr/bin/env bash 

... if you made sure your script contains no BOM and if you made sure that it's executable:

chmod +x daily_ori.sh

... and used a relative path (or added your current path to PATH, which - however - is a bad idea!):

sh ./daily_ori.sh

... this should succeed. Unless there is more information that is lacking from your question.

But that last step really calls into question why you insist on executing the script with sh in the first place if your intention is to invoke Bash. Simply use a hashbang (whether hardcoded or with env) to locate bash as interpreter, make sure the script is executable and call it as you normally would ...


To check for the BOM use:

xxd -g 1 daily_ori.sh|head -n 2

... and edit that into your question.

6
  • Updated question please check
    – Abdul Moiz
    Commented May 19, 2020 at 11:38
  • No BOM, so the problem is solved then. Commented May 19, 2020 at 11:40
  • Can you explain? how? I am still unable to run this bash script?
    – Abdul Moiz
    Commented May 19, 2020 at 11:41
  • @AbdulMoiz yes, I updated my answer accordingly. You keep running your script with sh as interpreter, I am not sure why you expect any different outcome? You need to execute it as a command, unless you know you can rely on sh being a particular shell/interpreter. And if you can't you must stick to the common denominator, which is POSIX shell dialect. Commented May 19, 2020 at 11:47
  • @AbdulMoiz : So the script works now ? Commented May 19, 2020 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .