English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This is the script.
I am getting these errors:
swap.sh: line 10: syntax error near unexpected token `then'
swap.sh: line 10: `then'

#first script
#script name is swap.sh
#check if correct number of arguments


if [ $# != 2 ]
then
echo "Error: wrong amount of arguments."
elif[ -f $1 ]||[ -f $2 ]
then
echo "Either the file" $1 "or the file" $2 "do not exist."
elif[ -r $1 ]||[ -r $2 ]
then
echo "Either the file" $1 "or the file" $2 "are not readable."
elif [ -w $1 ]||[ -w $2 ]
then
echo "Either the file" $1 "or the file" $2 "are not writable."
else
mv $1 temp1
mv $2 temp2
mv temp1 $2
mv temp2 $1
rm temp1
rm temp2
echo "The file" $1 "and the file" $2 "where succesfully swapped."
fi
exit

2007-03-24 14:57:57 · 1 answers · asked by adrenalineguy87 2 in Computers & Internet Programming & Design

Dude SteveN you are the freaking man. I changed the details you told me and used the test format i think its easier. I always have questions about unix script and C++ programming so I will be counting on you to answer several of my future questions. Thanks again man, you saved my ***!

2007-03-24 16:19:59 · update #1

1 answers

There are a couple of problems with this script, but it is nothing major. You were on the right track.

First, every shell command (such as "elif") and UNIX command has a certain syntax. I noticed that you did not leave a space after two of the elif commands. That is why you were getting the syntax error.

Next, the echo commands are telling me that you want to check if the files do NOT exist, or are NOT readable or writable. When doing the if test, the symbol for NOT is an exclamation point (!) which would have to be included before the -f -r or -w.

Finally, the last thing is that you are using the mv command which is a "move" or "rename" of the files. Because you are doing this, there will be no temp1 and temp2 file to delete with the rm command, so you will get warnings or errors about these files not existing.


On an advanced note, you are using a fixed name to do the swap (temp1 and temp2). What happens if two people try to use swap.sh at the SAME time in the same directory? They may interfere with each other and overwrite each other's files. It is good that you are doing all the extra checking if the files exist and are readable and such, but it is important to also check the success of the mv commands, and the existence of temp files.

You already know about the $1 and $# , but did you know that $$ = the current PID of your process and $? = the exit status of the command? You could use this $$ in the filename of your temporary files to make sure that they are unique. For example:

mv $1 temp1.$$
mv $2 temp2.$$
mv temp2.$$ $1
mv temp1.$$ $2

You also only really need one tempfile, since once the first file has been renamed, the second file can become the first file immediately:

mv $1 temp.$$
mv $2 $1
mv temp.$$ $2

And I don't know if this will help you or confuse you, but there are two standards to doing the if statement tests. The way you are doing it requires the use of square brackets but the other standard is as follows:

if test $# != 2
then
echo "Error: wrong amount of arguments."
elif test ! -f $1 -o ! -f $2
then
(and so on)


You may want to get yourself a copy of the O'Reilly and Associates book on shell scripting that matches the version that you are using (Korn Shell, Bourne Shell, etc..) They are a great help.

2007-03-24 15:34:47 · answer #1 · answered by SteveN 7 · 0 0

fedest.com, questions and answers