Twisted this way, nothing better (I didn’t really like it myself). But, without shift, spaces in the parameter values do not interfere.
if [ $# -gt 1 ]; then let last=$# let plast=$last-1 echo nargs = $# lastno = $last plastno = $plast namel=`echo $# | awk '{printf "${%s}",$1}'` vlast=`eval echo $namel` namepl=`echo $plast | awk '{printf "${%s}",$1}'` vplast=`eval echo $namepl` echo last arg = \'$vlast\' before-last = \'$vplast\' else echo not enought fi
The names are stupid, but I think the idea with eval
is clear.
UPD @myqck , read your question more closely. Look at copying. Not sure if this is a common pattern , but IMHO is working.
#!/bin/sh # copy files from dir1 to dir2 # if [ $# -lt 3 ]; then echo Usage: my-cp dir1 dir2 dir1-filenames-list exit 1 fi dir1=$1; shift dir2=$1; shift #echo dir1 = \'$dir1\' dir2 = \'$dir2\' files: $* if [ -d $dir1 ]; then # Это по желанию. Если dir2 должно существовать, выкиньте # и поправьте echo в if mkdir -p $dir2 if [ ! -d $dir2 ]; then echo Can\'t create $dir2 exit 2 fi # copy files with user,permitions,times ... (cd $dir1; tar cf - $*) | (cd $dir2; tar xf -) # другой вариант с cp # dir2=`(cd $dir2; pwd)` # cd $dir1; # cp -R $* $dir2 else echo No source directory: $dir1 exit 1 fi
It may be useful to you. To simplify programming, I changed the order of the arguments and allowed copying the hierarchy (if there are tables of contents in the list of copied files)
my-cp source-dir target-dir files-list-in-source-dir
IMHO is also quite convenient for the user. If the target is not, try to make it.
If copying the hierarchy does not fundamentally suit you, then simply use the copy option with cp
and discard -R.
For the variant with tar, you will have to modify the list (run through it, check the file type and if the item is not a file, then shift).