linux - Copy multiple files with bash script from command line arguments? -


i want create script allows me enter multiple filenames command line, , have script copy files directory. trying keep getting error of

line 10: binary operator expected

#!/bin/bash  directory=/.test_files file=$* if [ -e $directory/$file ];         echo "file exists" else         cp $file $directory fi 

so if script named copfiles.sh, writing...

./copyfiles.sh doc1.txt doc2.txt 

it move files, if exist, won't read error message.

also "line 10: binary operator expected" error regardless of files there or not. can tell me doing wrong?

as possible problem, if had filename space or had multiple arguments $* have spaces in [ -e $dir/$file ] expand have lots of words, [ -e /.test_files/first word , more ] , -e expects 1 word after it. try putting in quotes like

if [ -e "$directory/$file" ] 

of course, may want store $1 in $file first argument.

to test arguments want loop on arguments , test each like

for file in "$@";     if [ -e "$directory/$file" ];         echo "$file exists"     else         cp "$file" $directory     fi done 

using quotes around $@ preserve spaces in original arguments well


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -