Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date command not found in shell script

Tags:

sh

my shell script backup.sh as below, when i execute this script, i got the error "date: command not found". I have checked that there are no space between = and the parameters. Could anydody give an explain about this error. Thanks.

x00004:/home/ # cat backup.sh 
#!/bin/sh
set +x

source ./conf.sh

if [ $# != 2 ] ; then
    echo " Usage: $0 [sftp user] [sftp pwd]"
    exit 1;
fi

DATE=`date +%y-%m-%d--%H:%M:%S`
echo "${DATE}"
file=test.tar.gz
echo "Starting to backup files..."

#to backup files to sftp server

x00004:/home/ # ./backup.sh usr pwd
./backup.sh: line 11: date: command not found

Starting to backup files...
x00004:/home/poc/src # 
like image 975
Ming Ding Avatar asked May 24 '26 18:05

Ming Ding


1 Answers

Shells can't find commands if the commands aren't in directories in $PATH. Quick fix: replace date with /bin/date. However, a similar problem might occur in conf.sh or backup.sh, so a more global solution is to echo "$PATH" at the beginning of your script, figure out what's missing, and and fix it, e.g., PATH=/bin:$PATH.

like image 121
webb Avatar answered May 30 '26 09:05

webb