我一直在研究这个项目已经有一段时间了,我无法弄清楚为什么如果你可以提供帮助那么这就行不通!请记住它是在linux中。
#!/bin/bash
echo "Welcome what would you like to do? you can:"
echo "Display a list of current users (L)"
echo "Display a list of files including hidden ones (H)"
echo "Display a calender for current month (C)"
echo "Quit the program (Q)"
echo "Remember to use caps"
while read option
do
case $option in
L) $ awk -F':' '{ print $1}' /etc/passwd ;;
H) ls -a ;;
C) cal ;;
*) echo "Unknown option": $option ;;
Q) echo "good bye"
;;
esac
done
答案 0 :(得分:3)
就像安德烈亚斯所说的那样:但是 - 解析passwd也不会告诉你谁在系统上当前有效。
这是一个经过修改的脚本。
#!/bin/bash
echo "Welcome what would you like to do? you can:"
echo "Display a list of all users (L)"
echo "Display a lst of current logged in users (U)"
echo "Display a list of files including hidden ones (H)"
echo "Display a calender for current month (C)"
echo "Quit the program (Q)"
echo "Remember to use caps"
while read option
do
case $option in
L) awk -F':' '{ print $1}' /etc/passwd ;;
U) who ;;
H) ls -a ;;
C) cal ;;
Q) echo "good bye"
exit ;;
*) echo "Unknown option": $option ;;
esac
done
答案 1 :(得分:2)
首先,在您致电$
之前,有一个虚假的awk
标志 - 请改用它:
...
L) awk -F':' '{ print $1}' /etc/passwd ;;
...
其次,您需要将*
案例移至case
条件的末尾,否则Q
将永远不会被调用,因为*
已经抓住了Q
。
第三,在good bye
案例中,您正在打印exit
消息,但您的脚本将继续运行 - 您需要 Q) echo "good bye"
exit
;;
脚本:
{{1}}