Bash - 如何使程序继续使用 if 语句而不是重复代码?
Bash - How to make a program continue on with an if statement rather than having to duplicate code?
我发誓我每天都能听到,对不起,如果我开始成为一个讨厌的人,无论如何这里有一些代码示例:
例如计划 "Ask Name"
if [ [=11=] -eq 0 ] || [ [=11=] -eq ]
then
if [ $# -eq 0 ]
then
echo "Sir I insist, I must know your name"
read
fi
#[the long code for if the number of arguments =1 goes here!!]
#the long code isn't important just imagine some fancy code here
#fancy code
#more fancy code
fi
基本上我只是想要它,以便在 if 语句之后用户可以以某种方式修改参数的数量,然后继续执行脚本的其余部分。
目前用户只需要不断地输入一些东西并且它永远不会停止,很可能是因为“$1”作为变量不起作用。
虽然我问这个的主要原因是找出是否有办法摆脱 if 语句并继续执行程序,即使输入的参数符合特定条件。
如果你不明白我的意思,再举一个例子:
#takes name as argument
if [ == "dave" ]
then
echo "I don't like people called Dave, give me another name"
[SOMETHING HERE THAT MAKES THIS INPUT, NOT SURE ABOUT 'read ']
else
echo "Your name is cool, come inside!"
fi
这个例子有点糟糕,但我基本上想要它,这样用户就可以从回显消息中继续代码,尽管被称为 Dave。
一如既往,我很感激任何建议。
[在标题中我提到了重复]
我基本上指的是不必复制所有其他代码并将其放在两个不同的 if 语句下。这是我目前唯一能想到的解决方案。
如果我正确理解你的问题,而不是 IF 语句,学习如何使用 case 可能会更好。 example 个案例:
space=`df -h | awk '{print }' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`
case $space in
[1-6]*)
Message="All is quiet."
;;
[7-8]*)
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % full."
;;
9[1-8])
Message="Better hurry with that new disk... One partition is $space % full."
;;
99)
Message="I'm drowning here! There's a partition at $space %!"
;;
*)
Message="I seem to be running with an nonexistent amount of disk space..."
;;
esac
如果我们使用您的第二个示例:
case in
dave)
echo "I don't like people called Dave, give me another name"
;;
*)
echo "Your name is cool, come inside!"
;;
esac
您似乎在寻找 set
命令:
if [ $# -eq 0 ]; then
echo "Sir I insist, I must know your name"
read name
set -- "$name"
fi
if [ $# -eq 1 ]; then
# This is guaranteed to be true if $# was originally 0
# It won't be true if the user original provided 2 or more arguments.
fi
if [ "" = "dave" ]
then
echo "I don't like people called Dave, give me another name"
read name
shift # discard the original first argument
set -- "$name" "$@"
else
echo "Your name is cool, come inside!"
fi
我发誓我每天都能听到,对不起,如果我开始成为一个讨厌的人,无论如何这里有一些代码示例:
例如计划 "Ask Name"
if [ [=11=] -eq 0 ] || [ [=11=] -eq ]
then
if [ $# -eq 0 ]
then
echo "Sir I insist, I must know your name"
read
fi
#[the long code for if the number of arguments =1 goes here!!]
#the long code isn't important just imagine some fancy code here
#fancy code
#more fancy code
fi
基本上我只是想要它,以便在 if 语句之后用户可以以某种方式修改参数的数量,然后继续执行脚本的其余部分。
目前用户只需要不断地输入一些东西并且它永远不会停止,很可能是因为“$1”作为变量不起作用。
虽然我问这个的主要原因是找出是否有办法摆脱 if 语句并继续执行程序,即使输入的参数符合特定条件。
如果你不明白我的意思,再举一个例子:
#takes name as argument
if [ == "dave" ]
then
echo "I don't like people called Dave, give me another name"
[SOMETHING HERE THAT MAKES THIS INPUT, NOT SURE ABOUT 'read ']
else
echo "Your name is cool, come inside!"
fi
这个例子有点糟糕,但我基本上想要它,这样用户就可以从回显消息中继续代码,尽管被称为 Dave。
一如既往,我很感激任何建议。
[在标题中我提到了重复]
我基本上指的是不必复制所有其他代码并将其放在两个不同的 if 语句下。这是我目前唯一能想到的解决方案。
如果我正确理解你的问题,而不是 IF 语句,学习如何使用 case 可能会更好。 example 个案例:
space=`df -h | awk '{print }' | grep % | grep -v Use | sort -n | tail -1 | cut -d "%" -f1 -`
case $space in
[1-6]*)
Message="All is quiet."
;;
[7-8]*)
Message="Start thinking about cleaning out some stuff. There's a partition that is $space % full."
;;
9[1-8])
Message="Better hurry with that new disk... One partition is $space % full."
;;
99)
Message="I'm drowning here! There's a partition at $space %!"
;;
*)
Message="I seem to be running with an nonexistent amount of disk space..."
;;
esac
如果我们使用您的第二个示例:
case in
dave)
echo "I don't like people called Dave, give me another name"
;;
*)
echo "Your name is cool, come inside!"
;;
esac
您似乎在寻找 set
命令:
if [ $# -eq 0 ]; then
echo "Sir I insist, I must know your name"
read name
set -- "$name"
fi
if [ $# -eq 1 ]; then
# This is guaranteed to be true if $# was originally 0
# It won't be true if the user original provided 2 or more arguments.
fi
if [ "" = "dave" ]
then
echo "I don't like people called Dave, give me another name"
read name
shift # discard the original first argument
set -- "$name" "$@"
else
echo "Your name is cool, come inside!"
fi