I just started shell coding and I'm a bit confused on how 'mv' works can someone explain to me how it works and if i did this correctly. Thanks.
echo "Enter Name of the first file:"
read file1
#echo $file1
if [! -e $file1]; then
echo "Sorry, file does not exist."
exit 1
fi
echo "Name of the second file:"
read file2
if [! -e $file2]; then
echo "Sorry, file does not exist."
exit 1
else
#echo $file1
#echo $file2
set tmp = $file1
mv "$file2" "$file1"
mv "$tmp" "$file2"
fi;;
once it gets down to the 'mv' i get
which confuses me since both files pass the if statements. Can someone clarify?
sorry for the long wait. Thanks. After fixing the spaces I have
echo "Enter Name of the first file:"
read file1
#echo $file1
if [ ! -f $file1]; then
echo "Sorry, file does not exist."
exit 1
fi
echo "Name of the second file:"
read file2
if [ ! -f $file2]; then
echo "Sorry, file does not exist."
exit 1
else
#echo $file1
#echo $file2
set tmp = $file1
mv "$file2" "$file1"
mv "$tmp" "$file2"
fi;;
But when I run it I still get this error
Am using the mv in the wrong manner?
(I'm trying to swap the name of the files.)
I dislike white spaces . . . thanks. Can anyone explain
set tmp = $file1
mv $file2 $file1
mv $tmp $file2
After the first "mv" is executed $file2 gets deleted so when the second "mv" command occurs there is no longer a $file2. Is there a way to not delete $file2 when it gets copied to $file1?
You should by now have a space after "[" and a space before "]".
The logic in this bit needs attention if you want to swap the filenames:
Maybe try:
# Rename both files to preserve their permissions.
mv "${file1}" "${file1}.TMP"
mv "${file2}" "${file2}.TMP"
# Cross-rename the files to their final names
mv "${file1}.TMP" "${file2}"
mv "${file2}.TMP" "${file1}"
BTW. I can't make "set tmp = $file1" do anything useful.
Some may argue that the filename swap can be done in three steps (not four). However this can introduce unexpected changes to file permissions.
Footnote: Notice the curly braces. You will rarely find cause to not use them!
Footnote 2: Providing both files are on the same filesystem a "mv" command just changes the name of the inode. It is very quick and the contents of the files do not move. If the files are on different filesystems they do actually move!
To cfajohnson. I did not expect this response from you who I recognise as an expert in shell syntax. The O/P script did not contain the problem, my initial response did not contain the problem, but the example was designed to teach.
Please explain the error message in my example in your own words.
Thanks cfajohnson. No offence taken or meant, but pedandtic or what? One day the O/P will appreciate this input and we may have to do a contract on that site.
Curly brackets separate the variable name from other text surrounding it. Eg.
echo ${foo}bar
means fetch variable foo's value and append it with text bar. Without curly brackets the shell would be searching for a variable called foobar. One could also write
My pedantic response would of course be to point to the strict definition of pedantic.
The Wiktionary definition is more in the spirit of current usage than strictly accurate: pedantic - Wiktionary
I'm never sure whether words mean the same on the other side of the Atlantic.
No offence taken or intended.