need help understanding mv

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?

try.. these with spaces.

if [ ! -e $file1 ]; then

although i would prefer it this way

if [ ! -f $file1 ]; then

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.)

you still have not followed the suggested change completely..

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!

Thanks guys. Can you briefly explain the curly brackets.

At a basic level the curly braces enclose the name of the variable such that it stands alone as an entity.

If I want to substitute the contents of the string into a variable or a command we can get problems:

file1="myfilename"
echo "$file1MYSTRING"

sh: file1MYSTRING: Parameter not set

This is because the shell doesn' know when the parameter name ends and the string starts.

file1="myfilename"
echo "${file1}MYSTRING"

myfilenameMYSTRING

Similarly distinguishing variables $file1 from $file11 which cropped up on this board recently.

Get it?

In the example shown, they are unnecessary.

They are only needed when the variable name is followed by a character that could be part of a variable name.

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.

I was not commenting on any error message. I was answering the question, "Can you briefly explain the curly brackets."

My comment was about this:

mv "${file1}" "${file1}.TMP"
mv "${file2}" "${file2}.TMP"

The braces are not necessary in that example. They are not wrong, but they do nothing.

If the example had been:

mv "${file1}" "${file1}_TMP"
mv "${file2}" "${file2}_TMP"

...then they would have been necessary but only in the second instance on each line.

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.

lol thanks guys. I greatly appreciate the clarification.

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

echo $foo bar

, but that would produce a space between.

Regards,

pen

I need to change my perscription. Just noticed this is old. Wish I could just delete. :wink:

Seen on Usenet:

"Pedantic is a word used to describe those who value accuracy by those who don't."

:wink:

Nice one cfajohnson .

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.