我正在编写一个bash脚本来指示用户信息的前三行。
前:
$ ./c.sh bob unknown
Login: bob Name: Bob
Directory: /u1/h7/bob Shell: /bin/tcsh
Office: AA 044, x8361 Home Phone: 000-000-0000
unknown: no such user.
到目前为止,这是我的代码
#!/bin/bash
if [ $# == 0 ]; then
echo "Usage: ./c.sh Login/Username"
exit
else
i=$#
j=1
while [ "$j" -le "$i" ]; do
finger ${$j} | head -n+3
echo
j=$(($j+1))
done
fi
而不是给命令行参数的用户类型,$ {$ j}给我$ j的值,有关如何获取登录名/用户名的任何建议和帮助?我试过$($ j),$((j)),$ {$ j} ....
答案 0 :(得分:2)
答案很简单:停止使用不必要的间接:
#!/bin/bash
if (( $# == 0 )); then
echo "Usage: ./c.sh Login/Username"
exit
else
while [[ $1 ]]; do
finger "$1" | head -n+3
echo
shift
done
fi
...或
…
for user; do # equivalent to `for user in "$@"; do`
finger "$user" | head -n+3
…
done
你可以这样写:
i=$#
j=1
while [ $j -le $i ]; do
finger "${@:j++:1}" | head -n+3
echo
done
......但你不需要那么努力。
答案 1 :(得分:0)
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage: $0 Login/Username"
exit
else
for ARG in "$@"; do
finger "$ARG" | head -n 3
echo # If you want a newline
done
fi
尽可能简单。