如何仅在特定条件下读取用户输入.以下代码在ksh中不起作用:
if [[ $contract = "$fld1" ]];then
read position?"Enter the position of Contract number in m-n format,m should be less than n "
fi
Run Code Online (Sandbox Code Playgroud)
在上述条件下代码不等待用户提示
发布的代码在ksh下运行正常.
$ contract=c
$ fld1=c
$ if [[ $contract = "$fld1" ]];then
> read position?"Enter the position of Contract number in m-n format,m should be less than n " <
> fi
Enter the position of Contract number in m-n format,m should be less than n 1-2
$ echo $position
1-2
Run Code Online (Sandbox Code Playgroud)
在回答hedgehog的评论时,你可能会使用一个不那么令人困惑的提示:
read position?"Enter the position of Contract number in m-n format, (m should be less than n) : "
Run Code Online (Sandbox Code Playgroud)
请注意,read variable?prompt语法是ksh特定的.根据bash你的使用read -p prompt variable.可用于大多数基于Bourne语法的shell的可移植方法printf "%s" "$prompt" ; read variable