Postman Posted December 2, 2004 Report Share Posted December 2, 2004 (edited) I'm trying to make a small program that will take files as arguments when starting the program then swap contents of two files (or swap filenames, either one is good..) but it also need to check if the two variables are indeed files before doing the swap. However since I'm extremely new with programming Linux I get stuck alot and this is one I haven't managed to solve and I would be glad if someone could point me in the right direction... Here's the code #! /bin/bash if [ -f $1 && -f $2 ] then mv $1 temp mv $2 $1 mv temp $2 rm temp echo "Files has been swapped" else echo "One or both variables isn't a file" fi I have set exec-rights but when I execute I get an error saying line 3: [: missing `] ' What is wrong with this picture? Edited December 2, 2004 by Postman Quote Link to comment Share on other sites More sharing options...
Steve Scrimpshire Posted December 2, 2004 Report Share Posted December 2, 2004 (edited) #! /bin/bash if [ -f $1 ] && [ -f $2 ] then mv $1 temp.bak mv $2 $1 mv temp.bak $2 echo "Files has been swapped" else echo "One or both variables isn't a file" fi you also don't need the 'rm temp' because mv removes it. Edited December 2, 2004 by Steve Scrimpshire Quote Link to comment Share on other sites More sharing options...
Postman Posted December 2, 2004 Author Report Share Posted December 2, 2004 Many thanks! Quote Link to comment Share on other sites More sharing options...
aru Posted December 2, 2004 Report Share Posted December 2, 2004 (edited) Just another way of doing the same. See small differences just to show you other alternatives #! /bin/bash TMPFILE=$(mktemp -q ~/tmp/$0.XXXXXX) [ $# != 2 ] && echo >&2 "Usage: $0 file1 file2" && exit 1 if [[ -f $1 && -f $2 ]]; then cat $1 > $TMPFILE cat $2 > $1 cat $TMPFILE > $2 echo "File contents have been swapped" else echo "One or both parameter(s) isn't a file" && exit 1 fi Edited December 2, 2004 by aru Quote Link to comment Share on other sites More sharing options...
theYinYeti Posted December 2, 2004 Report Share Posted December 2, 2004 Very interesting aru! I did not know of this mktemp thing. One small detail: it usually is better to quote filenames: cat "$1" >"$TMPFILE" For the record, I seem to remember another technique for not using more space on disk. It's not always better (not here for example if you use Steve's suggestion), but still good to know: local HERETMP="$(dirname "$1")/.swap.tmp" ln "$1" "$HERETMP" mv -f "$2" "$1" mv "$HERETMP" "$2" This requires, however, that $1 and $2 be files, and on the same filesystem (two limitations of hard links). Yves. Quote Link to comment Share on other sites More sharing options...
aru Posted December 2, 2004 Report Share Posted December 2, 2004 (edited) One small detail: it usually is better to quote filenames:cat "$1" >"$TMPFILE" I agree 100%, it is just that I trust always my filenames :). The only 'but' is when any kind of 'shell expansions' is involved such tilde expansions, but since it doesn't apply here, using quotes is always safer than doesn't and should be taken as a general rule. For the record, I seem to remember another technique for not using more space on disk. [...] Sure, using hardlinks here is clever. And that has remember me a very old tip published in the old board about "ln": how 'ln' can make a better understanding of Unix It has been nice to read it again,thanks Edited: corrected typos Edited December 2, 2004 by aru Quote Link to comment Share on other sites More sharing options...
aru Posted December 4, 2004 Report Share Posted December 4, 2004 #! /bin/bash[ $# != 2 ] && echo >&2 "Usage: $0 file1 file2" && exit 1 if [[ -f "$1" && -f "$2" ]]; then HAM=$(< "$1"); cat "$2" > "$1"; echo "$HAM" > "$2" echo "File contents have been swapped" else echo >&2 "One or both parameter(s) isnt a file" && exit 1 fi Nothing new under the sun, not clever indeed, but yet another way, this time using a variable to hold temporary the contents of file1 ...very bored this morning... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.