linux – 在Bash中将多个用户输入添加到一个变量中
发布时间:2020-12-14 00:43:49 所属栏目:Linux 来源:网络整理
导读:我是unix bash脚本的新手,需要知道这是否可行.我想多次询问用户输入,然后将输入存储到一个变量中. userinputs= #nothing at the startread stringcode to add $string to $userinputsread stringcode to add $string to $userinputs #this should add this i
我是unix bash脚本的新手,需要知道这是否可行.我想多次询问用户输入,然后将输入存储到一个变量中.
userinputs= #nothing at the start read string <code to add $string to $userinputs> read string <code to add $string to $userinputs> #this should add this input along with the other input 因此,如果用户在第一次询问时输入“abc”,则会在$userinputs中添加“abc” 然后当再次询问输入并且用户输入“123”时,脚本应该将其存储在相同的$userinputs中 这会使$userinput = abc123 解决方法
在Bash中连接两个字符串的常用方法是:
new_string="$string1$string2" 只有当我们有一个可以阻碍变量扩展的文字字符串时,才需要在变量名称周围添加{}: new_string="${string1}literal$string2" 而不是 new_string="$string1literal$string2" 你也可以使用=运算符: userinputs= read string userinputs+="$string" read string userinputs+="$string" 在这种情况下,双引号$string是可选的. 也可以看看: > How to concatenate string variables in Bash? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |