I'm trying to write a code that moves files to a .junk directory using the built-in environment variables. My goal is that all a user would have to do is type sh junk filename1 filename2 filename3... and the script would use $1, $2, $3... to move the files. Unfortunately I can't quite get the counter loop to work correctly.
#!/bin/s
if [ $# = 0 ]
then echo "Usage: junk -l | -p | { filename }*"
elif [ $1 = "-l" ] && [ $HOME/.junk ]
then ls $HOME/.junk
elif [ $1 = "-p" ] && [ $HOME/.junk ]
then rm -r $HOME/.junk
elif [ $1 = "$@" ] && [ $HOME/.junk ]
then echo $#
i=1
s="$"
while [ $i -le $# ]
do
echo $s$i
mv $s$i .junk/$i
i=$(($i+1))
done
fi
/bin/s
? – Anthon Apr 28 at 21:36$1
should always be quoted:"$1"
. What is[ $HOME/.junk ]
supposed to be/do? And[ $1 = "$@" ]
? Why doeswhile [ $i -le $# ]
start withi=1
instead ofi=0
? What is supposed to happen with a single file argument?mv $s$i
is probably supposed to be an (unquotes, of course) indirection which would have to be done witheval
or (better)"${!i}"
... The counter loop really is the least of your problems. – Hauke Laging Apr 28 at 21:44mv "$@" .junk
. Could you explain what your script is doing and why you want the loop? – terdon♦ Apr 28 at 21:52mv "$@" ./junk
will present argument list size problems, but that would take a lot of junk. – mikeserv Apr 29 at 0:07