Hello Friends,
I am writing a shell script which will grab a file if it exists and copies it to another folder and will append with current date. I have written but gives me error, plz help:
--------------------------------------------
#!/usr/bin/sh
source=/home/dev4rice/naveen/test1
target=/home/dev4rice/naveen/test2
file_name=$source/text.txt
target_file=$target/text.txt
if [-f $file_name]
cp $source/text.txt $target
chmod $target/text.txt 777
fi
cp $target_file $target_file`date +%y%m%d`
--------------------------------------------------
Error I am getting is :
./copy_file.sh[8]: 0403-057 Syntax error at line 14 : `fi' is not expected.
if [-f $file_name]; then
cp $source/text.txt $target
chmod 777 $target/text.txt
fi
I fixed as per your suggestions but still getting following error -
$ ./copy_file.sh
./copy_file.sh[8]: [-f: not found.
cp: /home/dev4rice/naveen/test2/text.txt: A file or directory in the path name does not exist.
#!/usr/bin/sh
source=/home/dev4rice/naveen/test1
target=/home/dev4rice/naveen/test2
file_name=$source/text.txt
target_file=$target/text.txt
if [-f $file_name];
then
mv $file_name $target
chmod 777 $target/text.txt
fi
cp $target_file $target_file`date +%y%m%d`
pay attention to spaces:
if [ -f $file_name ]; then
cp $source/text.txt $target
chmod 777 $target/text.txt
fi
Its working now thanks
#!/usr/bin/sh
source=/home/dev4rice/naveen/test1
target=/home/dev4rice/naveen/test2
file_name=$source/text.txt
target_file=$target/text.txt
if [ -e $file_name ];
then
cp $file_name $target
chmod 777 $target/text.txt
fi
cp $target_file $target/text_`date +%m%d%Y`.txt