Hi,
I am using solari 10 OS which is having bash shell.
I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory.
example:I hava a machine1 which is having some files in a directory /spa/5.0/incude/file1 etc.
say I have user1. I want to give user1 and name of the file or directory say file1or dir1 as input to the script .based on the input given ,script should search for the filesor directory recursively in the /spa/5.0/incude directory and copy the files to the user home directory.
If files or directory not found then it should prompt a message saying directory or file does not exist.
#$1 space separated file/directory names
if [ $# -eq 0 ]; then
printf "specify atleast one file or directory name to copy\n"
else
while read fdname
do
if [ ! `find /spa/5.0/incude/ -xdev -name $fdname` ]; then
printf "$fdname not found\n"
else
find /spa/5.0/incude/ -xdev -name $fdname | while read file
do
cp -r $file ~
done
fi
done < $1
fi
Sorry for the delayed response.I have tried the following script and got the folllowing error.
-bash-3.00$ sh testscript.sh sa0_upd_estd_shipmn.shx
testscript.sh: sa0_upd_estd_shipmn.shx: cannot open
I have given full permission to the file sa0_upd_estd_shipmn.shx and the file is present in /spa/5.0/include directory.
still it is giving error.appreicate any help on it.
Extremely sorry for the typo error.Actual directory is /spa/5.0/include .I have done this change in the script i.e replacing /spa/5.0/incude with /spa/5.0/include but there is no luck.
output:
-bash-3.00$ more testscript.sh
if [ $# -eq 0 ]; then
printf "specify atleast one file or directory name to copy\n"
else
while read fdname
do
if [ ! `find /spa/5.0/include/ -xdev -name $fdname` ]; then
printf "$fdname not found\n"
else
find /spa/5.0/include/ -xdev -name $fdname | while read file
do
copy $file ~
done
fi
done < $1
fi
-bash-3.00$ sh testscript.sh sa0_upd_estd_shipmn.shx
testscript.sh: sa0_upd_estd_shipmn.shx: cannot open
In the syntax you post, $1 would refer to the name of a file from which you want to read the contents into the input stream in the script. This is not correct.
In your case I think that $1 contains a filename (or directory name) which you want to check whether it exist - and if it does, copy the file (or directory) to the user's home directory.
What is not clear is what happens when the parameter is a directory? Is the script going to attempt to copy that directory and all its contents or just copy the directory file?
In your case I think that $1 contains a filename (or directory name) which you want to check whether it exist - and if it does, copy the file (or directory) to the user's home directory--your statment is correct and if the mentioned directory or file does not exist it should throw an error message
What is not clear is what happens when the parameter is a directory? Is the script going to attempt to copy that directory and all its contents or just copy the directory file-------Requirement here is even if it is a directory it should traverse through the subdirctories and copy the subdirectories along with the files to the present directory or to the given input directory say /export/home/user1.i guess that point is not present in the script.
if [ $# -eq 0 ]; then
printf "specify atleast one file or directory name to copy\n"
else
fdname=$1
dest=$2
if [ "$dest" = "" ];then
printf "no destination specified, file will be copied to current directory\n"
dest=.
fi
if ! find /spa/5.0/include/ -xdev -name $fdname; then
printf "$fdname not found\n"
else
find /spa/5.0/include/ -xdev -name $fdname | while read file
do
cp -R $file $dest
done
fi
fi