所以这是我在上一个问题中已经问过的脚本问题: 链接:Typesort Shellscript in Unix
所以现在下面是我的脚本的更新版本,我的新问题是,如何更改它以将多个文件作为args传递来对这些文件进行排序?
还有没有更容易的选择来接受“ ./typesort -rnv而不是-r -n -v”之类的参数? 我觉得我的解决方案不是解决这个问题的最佳方法。
感谢您的答复。
#!/bin/bash
#A shell script with in- and output
usage()
{
cat <<EOF
$typesort [OPTIONS]
Sort filenames by file type given by
the ''file'' command
$typesort --version Print version number
OPTIONS:
-t sort text files only
-n sort non-text files only
-r reverse sort order
-v --verbose print debugging messages
EOF
}
# ----------------------------------------------
#flags
tflag=0
nflag=0
rflag=0
vflag=0
bflag=0
# -------------------------------------------------
sort_nA()
{
if [ "$vflag" == 1 ]
then
echo "sort_nA"
echo "file * | sort -t':' -k 2"
fi
file * | sort -t':' -k 2
}
# #######################################################
sort_r()
{
if [ "$vflag" == 1 ]
then
echo "sort_r"
echo "file * | sort -t':' -k 2 -r"
fi
file * | sort -t':' -k 2 -r
}
# #######################################################
sort_t()
{
if [ "$vflag" == 1 ]
then
echo "sort_t"
echo "file * | grep "ASCII text" | sort -t':' -k 2"
fi
file * | grep "ASCII text" | sort -t':' -k 2
}
# #######################################################
sort_n()
{
if [ "$vflag" == 1 ]
then
echo "sort_n"
echo "file * | grep -v "ASCII text" | sort -t':' -k 2"
fi
file * | grep -v "ASCII text" | sort -t':' -k 2
}
# #######################################################
sort_rt()
{
if [ "$vflag" == 1 ]
then
echo "sort_rt"
echo "file * | grep "ASCII text" | sort -t':' -k 2 -r"
fi
file * | grep "ASCII text" | sort -t':' -k 2 -r
}
# #######################################################
sort_rn()
{
if [ "$vflag" == 1 ]
then
echo "sort_rn"
echo "file * | grep -v "ASCII text" | sort -t':' -k 2 -r"
fi
file * | grep -v "ASCII text" | sort -t':' -k 2 -r
}
# #######################################################
# main
while true
do
if [ "$1" == "-t" ]
then
tflag=1
elif [ "$1" == "-n" ]
then
nflag=1
elif [ "$1" == "-r" ]
then
rflag=1
elif [ "$1" == "-v" ]
then
vflag=1
else
#at least 1 arg, let's check it
case $1 in
"-h" | "--help") #display help text
bflag=1
usage
break
;;
"--version") #display version number
echo "version number 0.1"
bflag=1
break
;;
"")
break
;;
"-rt" | "-tr")
rflag=1
tflag=1
;;
"-rn" | "-nr")
rflag=1
tflag=1
;;
"-rv" | "-vr")
rflag=1
vflag=1
;;
"-vt" | "-tv")
tflag=1
vflag=1
;;
"-vn" | "-nv")
nflag=1
vflag=1
;;
"-tn" | "-nt")
tflag=1
nflag=1
"-rtn" | "-trn" | "-tnr" | "-rnt" | "-nrt" | "-ntr")
rflag=1
tflag=1
nflag=1
;;
"-vtn" | "-tvn" | "-tnv" | "-vnt" | "-nvt" | "-ntv")
vflag=1
tflag=1
nflag=1
;;
"-rvn" | "-vrn" | "-vnr" | "-rnv" | "-nrv" | "-nvr")
vflag=1
rflag=1
nflag=1
;;
"-rvt" | "-vrt" | "vtr" | "-rtv" | "-trv" | "-tvr")
vflag=1
rflag=1
tflag=1
;;
"-rvtn" | "-rtvn" | "-rtnv" | "-rvnt" | "-rnvt" | "-rntv" | "-vrtn" | "-vtrn" | "-vtnr" | "-vrnt" | "-vnrt" | "-vntr" | "-trvn" | "-tvrn" | "-tvnr" | "-trnv" | "-tnrv" | "-tnvr" | "-nrtv" | "-ntrv" | "-ntvr" | "-nrvt" | "-nvrt" | "-nvtr")
vflag=1
rflag=1
tflag=1
nflag=1
;;
*) #anything else not valid
echo "Invalid option: §1"
;;
esac
fi
shift
done
if [ "$vflag" == 1 ]
then
echo "tflag = $tflag"
echo "nflag = $nflag"
echo "rflag = $rflag"
echo "bflag = $bflag"
fi
if [ "$bflag" == 1 ]
then
bflag=1 # nothing should happen
elif [ "$rflag" == 1 ] && [ "$tflag" == "$nflag" ]
then
sort_r
elif [ "$rflag" == 1 ] && [ "$tflag" == 1 ]
then
sort_rt
elif [ "$rflag" == 1 ] && [ "$nflag" == 1 ]
then
sort_rn
elif [ "$tflag" == "$nflag" ]
then
sort_nA
elif [ "$tflag" == 1 ]
then
sort_t
elif [ "$nflag" == 1 ]
then
sort_n
fi
exit 0
答案 0 :(得分:1)
只需在
之间包装脚本主体for arg in "$@"
do
..... # your script body goes here
done
并使用$ arg代替$ 1。然后,您可以显式地和/或使用文件名生成来传递几个参数:
your_script.sh file1 file2 other_files* file3
答案 1 :(得分:0)
执行脚本外壳程序
./Yourshellscript.sh file1 file2 ..