I am writing a program printing the file and directory of a specify directory. But I have to ignore certain kind of file. We store them in .ignorefile.
So, to get just the file that I want, I use find function. But, I have the same error every time: No such file or directory.
And, when I try on my terminal it works perfectly with the same arguments.
An example :
In my ignore file I have:
*.txt, *.cppWhen I execute my
.shfile I put two arguments: the ignore file and a directory.Then, when I scan all text in my file to construct the argument files then I call my
store()function.
Here you can see my code:
function store(){
dir="$1"
find_arg="$2" # Dir ! -name '*.txt' ! -name '*.cpp' -print
echo $find_arg
cmd=`find "$find_arg"`
echo $cmd
}
Here the function which build find_args :
function backup()
{
if [ $1="-i" ] && [ -f $2 ]
then
backignore=$2
if [ $3="-d" ] && [ -d $4 ]
then
directory=$4
find_arg=" $directory"
while IFS='' read -r line || [[ -n "$line" ]]; do
find_arg+=" ! -name "
find_arg+="'"
find_arg+=$line
find_arg+="'"
done < "$backignore"
find_arg+=" -print"
store $directory "$find_arg"
else
echo "please entrer a right directory"
fi
else
echo "Please enter a right ignore file"
fi
}
backup $1 $2 $3 $4
I call "sh" file
./file.sh -i ignorefile -d Source
output:
Source ! -name '\*.cpp' ! -name '\*.txt' -print
Source/unix_backup/example_files/backignore
Source/unix_backup/example_files/Documents/rep1/file3.txt
Source/unix_backup/example_files/Documents/rep1/file4.cpp
Source/unix_backup/example_files/Documents/rep3
Source/unix_backup/example_files/Documents/rep3/rep4
Source/unix_backup/example_files/Documents/rep3/rep4/file7.txt
Source/unix_backup/example_files/Documents/rep3/rep4/file8.cpp
find "$dir" $find_arg. don't use quotes around $find_arg because you want it to add multiple args to the command line, not just one quoted arg.cmd=`find $find_arg`