#!/bin/bashif [ $# !=1 ]then   echo Usage: A single argument which is the directory to backup   exitfiif [ ! -d ~/projects/$1 ]then   echo 'The given directory does not seem to exist (possible typo)'   exitfidate=`date +%F`if [ -d ~/projectbackups/$1_date ]then  echo 'this project has been backed up today, overwrite?'  read answer  if [ $answer != 'y' ]  then    exit  fielse  mkdir ~/projectbackups/$1_$dateficp -R ~/projects/$1 ~/projectbackups/$1_$dateecho Backup of $1 completedAnd i created sub-folder result under projects folder like this:[root@ip-10-0-7-125 result]# pwd/root/projects/resultBut when executing the script, it always throw a error:[root@ip-10-0-7-125 bash-tut]# ./pj-backup.sh /root/projects/result/./pj-backup.sh: line 2: [: 1: unary operator expectedThe given directory does not seem to exist (possible typo)[root@ip-10-0-7-125 bash-tut]# ./pj-backup.sh resutl./pj-backup.sh: line 2: [: 1: unary operator expectedThe given directory does not seem to exist (possible typo)Can you guys help me how to fix it, please?
Why this script didn't work?
linux;shell
On line 2, you have missed a space between the conditional operator != and the argument (1):[ $# != 1 ]Example:$ set -- foo bar$ [ $# !=1 ] && echo OKbash: [: 2: unary operator expected$ [ $# != 1 ] && echo OKOKAlso you are necessaroly doing string comparison, which can fail in many such cases e.g. what if the output of $# (replace this with some other variable) was designed to be shown as 01 instead of 1. So while doing arithmetic comparisons, use arithmetic comparison operator, -ne in this case:[ $# -ne 1 ]Or(( $# != 1 ))