[英]Setting a variable in a one-liner Bash script
Looking for a way to set a variable, in a bash one-liner, that is a script, such as: 寻找一种设置变量的方法,在bash one-liner中,这是一个脚本,例如:
export class={read -p "What Is Your Profession?" a; case $a in "Theif") echo "Stealth" ; in "Cleric") echo "Heals?" ; "Monk") echo "Focus?" ; *) echo "invalid choice" a; esac}
Although I'm having issues running this command without setting it to a one-liner, I've tried it numerous ways and I have gotten no results. 虽然我在运行此命令时遇到问题而没有将其设置为单行,但我已经尝试了很多方法而且我没有得到任何结果。 The above was just the most clearly laid out in my eyes.
以上是我眼中最清晰的一点。 I've also tried
VAR= ,
我也试过
VAR= ,
The case itself gives me a 案件本身给了我一个
-jailshell: syntax error near unexpected token `('
whenever I run it with more than one case. 每当我运行多个案例时。 I know this is probably all jumbled up.
我知道这可能都是混乱的。
You need double-semicolons ;;
你需要双分号
;;
to separate the clauses of a case
statement, whether it is one line or many. 分离
case
陈述的条款,无论是一行还是多行。 You also have to be careful with { … }
because it is used for I/O redirection. 您还必须小心使用
{ … }
因为它用于I / O重定向。 Further, both {
and }
must be tokens at the start of a command; 此外,
{
和}
必须是命令开头的标记; there must be a space (or newline) after {
and a semicolon (or ampersand, or newline) before }
. 在
{
和分号(或&符号或换行符) }
之后必须有空格(或换行符) }
。 Even with that changed, the assignment would not execute the code in between the braces. 即使改变了,分配也不会在大括号之间执行代码。 For that, you could use command substitution:
为此,您可以使用命令替换:
export class=$(read -p "What Is Your Profession?" a; case $a in "Theif") echo "Stealth" ;; "Cleric") echo "Heals?" ;; "Monk") echo "Focus?" ;; *) echo "invalid choice $a";; esac)
Finally, you've not run a spell-check on 'Thief'. 最后,你没有对'小偷'进行拼写检查。
Here is one-liner you can use: 这是您可以使用的单行程:
export class=`{ read -p "What Is Your Profession? " a; case $a in "Theif") echo "Stealth";; "Cleric") echo "Heals?";; "Monk") echo "Focus?";; *) echo "invalid choice" a;; esac; }`
OR better you can just put this inside a function: 或者你可以把它放在一个函数中:
function readclass() { read -p "What Is Your Profession? " a; case $a in "Theif") echo "Stealth";; "Cleric") echo "Heals?";; "Monk") echo "Focus?";; *) echo "invalid choice" a;; esac; }
Then use it: 然后使用它:
export class=$(readclass)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.