我可以使用该case
语句来处理参数吗?因此,使用 -restart 我想执行“-umount”和“-mount”
#!/bin/bash
case "$1" in
-mount
mount /ip/share1 /local/share1
;;
-umount
umount /ip/share1
;;
-restart
# echo TODO
;;
*)
[...]
esac
答案1
在我看来,除了缺少)
s 的语法问题之外,这应该可行。我对此进行了测试,它的行为正确..
#/bin/bash
case "$1" in
"-mount")
mount /path/to/device /path/to/mountpoint
;;
"-unmount")
umount /path/to/mountpoint
;;
"-remount")
"$0" -unmount
"$0" -mount
;;
*)
echo "You have failed to specify what to do correctly."
exit 1
;;
esac
答案2
#!/bin/bash
unset u
mnt() { ${u+u}mount /ip/share1 ${u-"/local/share1"}; }
case "$1" in
(-mount) :;;
(-umount) u= ;;
(-restart) u= mnt ;;
(*) ! :;;
esac && mnt
您可以使用如^上述^所示的函数。