在GNU Bash 4.3的Bash shell脚本中提示用户混淆

时间:2016-07-07 05:07:46

标签: linux bash shell

我正在尝试创建一个shell脚本,为Laravel应用设置我的Ubuntu服务器。在继续从此处获取以下代码之前,要求用户确认:

How do I prompt a user for confirmation in bash script?

#!/bin/sh
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' 

echo "\n ${GREEN}Enter the folder name for the Laravel application: ${NC}"
read APP_NAME


read -r -p "Are you sure? [y/N] " response
response=${response,,}    # tolower
if [[ $response =~ ^(yes|y)$ ]]
then
    echo "Installing dependencies..."
else
    exit
fi

我收到此错误:

Bad substitution 

就行了

response=${response,,}    # tolower

2 个答案:

答案 0 :(得分:0)

这是案例修改替代。以下是描述(来自shell parameter expansion上的Bash手册):

  

${parameter^pattern}
  ${parameter^^pattern}
  ${parameter,pattern}
  ${parameter,,pattern}

     

此扩展修改了字母字符的大小写   参数。扩展模式以生成模式   文件名扩展。参数展开值中的每个字符   针对模式进行测试,如果它与模式匹配,则其情况如下   转换。该模式不应尝试匹配多个   字符。 “^”运算符会转换匹配的小写字母   模式为大写; ','运算符转换匹配的大写   字母小写。 “^^”和“,,”扩展转换每个   扩展值中匹配的字符; '^'和','扩展   匹配并仅转换展开值中的第一个字符。如果   模式被省略,它被视为'?',它匹配每个   字符。如果参数为“@”或“*”,则为大小写修改操作   依次应用于每个位置参数,并且扩展为   结果清单。如果parameter是一个下标的数组变量   “@”或“*”,案例修改操作适用于每个成员   依次是数组,扩展是结果列表。

这适用于bash> = 4.0。

或者,您可以使用

response=$(echo "$response" | tr '[:upper:]' '[:lower:]')

答案 1 :(得分:0)

感谢David C. Rankins在评论部分的帮助,通过更改以下问题解决了这个问题:

#!/bin/sh

#!/bin/bash

并改变

echo "string data"

echo -e "string data"