简体   繁体   English

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

[英]Prompt user confimation in Bash shell script for GNU Bash 4.3

I'm trying to create a shell script that sets up my Ubuntu server for a Laravel app. 我正在尝试创建一个shell脚本,为Laravel应用程序设置我的Ubuntu服务器。 The user is asked to confirm before proceeding with the following code taken from here: 在继续从此处获取以下代码之前,要求用户确认:

How do I prompt a user for confirmation in bash script? 如何在bash脚本中提示用户进行确认?

#!/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

I'm getting this error: 我收到这个错误:

Bad substitution 

on the line 在线上

response=${response,,}    # tolower

This is a case modification substitution. 这是一个案例修改替代。 Here is the description (from the Bash manual on shell parameter expansion ): 这是描述(来自shell参数扩展的Bash手册):

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

This expansion modifies the case of alphabetic characters in parameter. 此扩展修改参数中字母字符的大小写。 The pattern is expanded to produce a pattern just as in filename expansion. 扩展模式以生成与文件名扩展一样的模式。 Each character in the expanded value of parameter is tested against pattern, and, if it matches the pattern, its case is converted. 参数的扩展值中的每个字符都针对模式进行测试,如果它与模式匹配,则转换其大小写。 The pattern should not attempt to match more than one character. 模式不应尝试匹配多个字符。 The ' ^ ' operator converts lowercase letters matching pattern to uppercase; ' ^ '运算符将匹配模式的小写字母转换为大写; the ' , ' operator converts matching uppercase letters to lowercase. ' , '运算符将匹配的大写字母转换为小写。 The ' ^^ ' and ' ,, ' expansions convert each matched character in the expanded value; ' ^^ '和' ,, '扩展转换扩展值中的每个匹配字符; the ' ^ ' and ' , ' expansions match and convert only the first character in the expanded value. ' ^ '和' , '扩展匹配并仅转换扩展值中的第一个字符。 If pattern is omitted, it is treated like a ' ? 如果省略pattern,则将其视为' ? ' ', which matches every character. ',匹配每个角色。 If parameter is ' @ ' or ' * ', the case modification operation is applied to each positional parameter in turn, and the expansion is the resultant list. 如果参数是' @ '或' * ',则将大小写修改操作依次应用于每个位置参数,并且扩展是结果列表。 If parameter is an array variable subscripted with ' @ ' or ' * ', the case modification operation is applied to each member of the array in turn, and the expansion is the resultant list. 如果parameter是一个用' @ '或' * '下标的数组变量,则大小写修改操作将依次应用于数组的每个成员,并且扩展是结果列表。

This works on bash >= 4.0. 这适用于bash> = 4.0。

Alternatively, you can use 或者,您可以使用

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

Thanks to David C. Rankins help in the comments section this issue was resolved by changing: 感谢David C. Rankins在评论部分的帮助,通过更改以下问题解决了此问题:

#!/bin/sh

to

#!/bin/bash

and changing 和改变

echo "string data"

to

echo -e "string data"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM