bash - Why doesn't my if statement with backticks work properly? -


i trying make bash script user able copy file, , see if done or not. every time copy done, or not, second output "copy not done" shown. idea how solve this?

if [ `cp -i $files $destination` ];then         echo "copy successful." else         echo "copy not done" fi 

what want is

if cp -i "$file" "$destination"; #... 

don't forget quotes.


you version:

if [ `cp -i $files $destination` ];then #.. 

will execute else branch.

the if statement in shell takes command. if command succeeds (returns 0, gets assigned $?), condition succeeds.

if if [ ... ]; then, it's same if test ... ; then because [ ] syntactic sugar test command/builtin.

in case, you're passing result of stdout* of cp operation argument test

the stdout of cp operation empty (cp outputs errors , go stderr). test invocation empty argument list error. error results in nonzero exit status , else branch.


*the $() process substitution or backtick process substitution slurp stdout of command run


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -