i have made a shell script and it runs until it reaches the if statement, doesn't the ! mean only if the command fails it will echo me that message and then exit
can anyone please help me what is wrong with my code?
many thanks,
rob
#!/bin/bash
echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
read method
case $method in
1)
echo "please specify full path to directory where the tar file is in"
read -e dir
cd $dir
echo "please copy/paste filename here of tar file (including .tar)"
read -e tar
echo "please enter ID number ie ID1234"
read id
echo "please specify where you want the tar file to be stored"
read -e dest
if ! mv $tar $id_$tar ; then
echo "something went wrong with the mv command, please do manually"
exit
fi
if ! rsync -avh $id_$tar $dest ; then
echo "something went wrong with the rsync command, please do manually"
exit
fi
if ! rm -f $id_$tar ; then
echo "something went wrong with the rm command, please do manually"
exit
fi
;;
2)
i have yet to do this one, but this is the directory one
;;
*)
echo "invalid selection, please re-run the script"
exit
;;
esac
You might be hitting a number of problems because you have minimal error checking and your mv command might have a null input so becomes invalid too. This might not be a problem for a simple helper script for yourself but might be more confusing if you delegate it.
Is a null response to the source directory okay? That would change to the user's home directory.
is this archive for an audio tar (press 1) or an audio directory (press 2)
1
please specify full path to directory where the tar file is in
/to_be_archived/
please copy/paste filename here of tar file (including .tar)
untitled\ folder.tar
please enter ID number ie ID1234
ID1234
please specify where you want the tar file to be stored
/archived_projects/
the above to get the directory or tar file all i need to do is tab as i have the e option which means interact with shell
Thank you for this. Can you run it again with bash -x your_script and paste the output. It will look messy, but it will show what your code is actually trying to do and what conditions are met in the if statements.
[root@robw-linux /]# bash -x test.sh
+ echo 'is this archive for an audio tar (press 1) or an audio directory (press 2)'
is this archive for an audio tar (press 1) or an audio directory (press 2)
+ read method
1
+ case $method in
+ echo 'please specify full path to directory where the tar file is in'
please specify full path to directory where the tar file is in
+ read -e dir
/to_be_archived/
+ cd /to_be_archived/
+ echo 'please copy/paste filename here of tar file (including .tar)'
please copy/paste filename here of tar file (including .tar)
+ read -e tar
untitled\ folder.tar
+ echo 'please enter ID number ie ID1234'
please enter ID number ie ID1234
+ read id
ID1234
+ echo 'please specify where you want the tar file to be stored'
please specify where you want the tar file to be stored
+ read -e dest
/archived_projects/
+ mv untitled folder.tar untitled folder.tar
mv: target `folder.tar' is not a directory
+ echo 'something went wrong with the mv command, please do manually'
something went wrong with the mv command, please do manually
+ exit
[root@robw-linux /]#
---------- Post updated at 09:10 AM ---------- Previous update was at 09:07 AM ----------
mmm... its not appending the ID number to it ie ID1234_untitled folder.tar
+ mv untitled folder.tar untitled folder.tar
mv: target `folder.tar' is not a directory
Don't escape in input, rather double quote the variables mv command mv "$var" "$var id"
Spaces and other 'special' chars are something you would want to avoid if possible when creating file system hierarchy.
Try to sanitize the input (ab)user puts in (like spaces or exclamation marks and such), then execute.
You might want to consider getopts shell builtin to create a script and pass your variables as options (which much more control, ability to automate later etc.)
As already pointed out by Peasant, the command mv untitled folder.tar untitled folder.tar sees four arguments due to the missing quotes around the variables to be expanded. On top, the shell expanded $id_ to the empty string, so source and target were identical leading to another error condition for mv .The "error log" output something went wrong with the mv command, please do manually was missing in posts #3 and #5, which was rather misleading in the analysis.
i worked it out but thank you for everyones help, i had to quote all the variables
#!/bin/bash
echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
read method
case $method in
1)
echo "please specify full path to directory where the tar file is in"
read -e dir
cd $dir
echo "please copy/paste filename here of tar file (including .tar)"
read -e tar
echo "please enter ID number ie ID1234"
read id
echo "please specify where you want the tar file to be stored"
read -e dest
if ! mv "$tar" "$id"_"$tar" ; then
echo "something went wrong with the mv command, please do manually"
exit
fi
if ! rsync -avh "$id"_"$tar" $dest ; then
echo "something went wrong with the rsync command, please do manually"
exit
fi
if ! rm -f "$id"_"$tar" ; then
echo "something went wrong with the rm command, please do manually"
exit
fi
;;
2)
i have yet to do this one, but this is the directory one
;;
*)
echo "invalid selection, please re-run the script"
exit
;;
esac
made a script and thought i would show you guys about it,
#!/bin/bash
echo "is this archive for an audio tar (press 1) or an audio directory (press 2)"
read method
case $method in
1)
echo "please specify full path to tar file"
read -e tar
base=$(basename "$tar")
echo "please enter ID number ie ID1234"
read id
echo "please specify where you want the tar file to be stored"
read -e dest
if ! mv "$tar" "$id"_"$base" ; then
echo "something went wrong with the mv command, please do manually"
exit
fi
if ! rsync -avh "$id"_"$base" "$dest" ; then
echo "something went wrong with the rsync command, please do manually"
exit
fi
if ! rm -f "$id"_"$base" ; then
echo "something went wrong with the rm -f command, please do manually"
exit
fi
;;
2)
echo "please specify full path to directory you want to be made into a tar"
read -e dir
cd $dir
cd ..
base=$(basename "$dir")
echo "please enter ID number ie ID1234"
read id
echo "please specify where you want the tar file to be stored"
read -e dest
if ! tar -cf "$id"_"$base".tar "$base" ; then
echo "something went wrong with the tar, please do manually"
exit
fi
if ! rsync -avh "$id"_"$base".tar "$dest" ; then
echo "something went wrong with the rsync, please do manually"
exit
fi
if ! rm -f "$id"_"$base".tar ; then
echo "something went wrong with the rm -f command, please do manually"
exit
fi
if ! rm -rf "$dir" ; then
echo "something went wrong with the rm -rf command, please do manually"
exit
fi
;;
*)
echo "invalid selection, please re-run the script"
exit
;;
esac