【发布时间】:2014-11-13 13:19:44
【问题描述】:
我有一个如下的 shell 脚本...
#!/bin/ksh
set_logging() {
if [ -f "$LOG_FILE" ]
then
mv $LOG_FILE $LOG_FILE.$SYNCDATE
else
touch $LOG_FILE
fi
# set logging for stdout and stderr (run this if user selects start)
exec >> $LOG_FILE
exec 2>&1
# run this if user selects dry
#exec something else
#exec something else
}
set_logging
# menu
if [[ ! -n $1 ]] ; then
clear
echo ""
echo "What are you trying to do?"
echo "start"
echo "dry"
echo "help"
echo ""
exit 99
fi
case "$1" in
start) run_tar;;
dry) nothing_here_yet;;
help) print_help;;
*) clear
echo "Your syntax is incorrect!"; exit 99;;
esac
这是实际脚本的片段,在 AIX 7.1 UNIX 操作系统上运行。
我要做的是基于用户在菜单上的选择,更改在 set_logging() 块中执行的部分(你会注意到我评论了我想要运行的代码,如果用户选择不同的菜单选项只是为了帮助这个例子)。
再次,这是一个简单的示例,以便我理解并可以在我的 shell 脚本的不同部分使用它。
有人可以帮忙吗?
【问题讨论】:
-
你已经知道如何根据变量的值做不同的事情了。您在该 sn-p 的底部使用它。只需将其应用于
set_logging内部。您还需要将"$1"传递给函数。 -
@EtanReisner 我想这就是他所期待的答案,你能把它贴在答案部分吗