我是Linux和Shell脚本的新手,我正试图编写一个Shell脚本,该脚本将根据传递的命令行参数显示目录列表。它将接受4个位置自变量,即F、D、T和P,并相应地显示。
样本输入和输出:
$ ./list.sh F D T P
File name date time permission
------------- ------ ----- ---------------
Filename1 date time permission
Filename2 date time permission
Total no. of files : <total number>
Total no of normal file : <number>
Total no of directory : <number>
争论可能会被打乱。我试着写一个shell脚本,它可以完成工作,但不能按我想要的方式显示。
#!/bin/bash
args=("$@") # Storing all the positional args into an array
n=0
x=0
while [ $n -ne 4 ]
do
case "${args[$n]}" in
F) argsVal[$x]=9 ;;
D) argsVal[$x]=6 ;;
T) argsVal[$x]=8 ;;
P) argsVal[$x]=1 ;;
*) echo "Invalid Option" ;;
esac
n=`expr $n + 1`
x=`expr $x + 1`
done
n=0
while [ $n -ne 4 ]
do
case "${argsVal[$n]}" in
1) echo -e "Permissions(P)tc" ;;
6) echo -e "Date(D)tc" ;;
8) echo -e "Time(T)tc" ;;
9) echo -e "FileNames(F)tc" ;;
*) ;;
esac
n=`expr $n + 1`
done
echo -e "n"
n=0
while [ $n -ne 4 ]
do
case "${argsVal[$n]}" in
1)
awk '{print $1}' listout ;;
6)
awk '{print $6,$7}' listout ;;
8)
awk '{print $8}' listout ;;
9)
awk '{print $9}' listout ;;
*) ;;
esac
n=`expr $n + 1`
done
输出为:
Permissions(P) Date(D) Time(T) FileNames(F)
-rw-r--r--
-rwxr--r--
-rwxr--r--
-rwxr--r--
-rwxr--r--
Jun 18
Jun 19
Jun 6
Jun 18
Jun 6
22:04
12:04
20:45
22:32
21:17
listout
list.sh
script.sh
test1.sh
validvoter.sh
更改最后一个while
循环代码
n=0
while [ $n -ne 4 ]
do
case "${argsVal[$n]}" in
1)
awk '{print $1}' listout ;;
6)
awk '{print $6,$7}' listout ;;
8)
awk '{print $8}' listout ;;
9)
awk '{print $9}' listout ;;
*) ;;
esac
n=`expr $n + 1`
done
至
awk -v a="${argsVal[0]}" -v b="${argsVal[1]}" -v c="${argsVal[2]}" -v d="${argsVal[3]}" '{printf("%st%st%st%sn", $a,$b,$c,$d)}' listout
一个awk
命令将扫描孔文件,它可以打印您想要的列。
参考man awk
,我们得到了一些类似的用法:
-v var=val
--assign var=val
Assign the value val to the variable var, before execution of the program begins. Such variable values are available to the BEGIN rule of an AWK program.
printf fmt, expr-list Format and print. See The printf Statement, below.
使用column
格式化列:
stat -c "%n %y %A" * | column -t
尝试使用printf
命令:
#!/bin/bash
while [ $# -gt 0 ] ; do
case $1 in
F)
arg="$arg$9, "
tit=("${tit[@]}" "File name (F)")
unl=("${unl[@]}" "-------------")
fmt="$fmt%-35s"
;;
P)
arg="$arg$1, "
tit=("${tit[@]}" "Permissions (P)")
unl=("${unl[@]}" "---------------")
fmt="$fmt%-17s"
;;
T)
arg="$arg$8, "
tit=("${tit[@]}" "Time (T)")
unl=("${unl[@]}" "--------")
fmt="$fmt%-10s"
;;
D)
arg="$arg$6" "$7, "
tit=("${tit[@]}" "Date (D)")
unl=("${unl[@]}" "--------")
fmt="$fmt%-10s"
;;
*)
echo "invalid option $1"
exit 1
esac
shift
done
if [ -z "$arg" ] ; then
echo "missing options"
exit 2
fi
arg=${arg%, } # remove trailing comma and space from $arg
printf "$fmt\n" "${tit[@]}"
printf "$fmt\n" "${unl[@]}"
ls -l | grep -v '^total' > listout
grep -v "^total" listout |
awk "{ printf "$fmt\n", $arg}"
tf=$(cat listout | wc -l)
tnf=$(grep '^-' listout | wc -l)
td=$(grep '^d' listout | wc -l)
echo
echo "Total number of files: $tf"
echo "Total number of normal files: $tnf"
echo "Total number of directories: $td"