简体   繁体   中英

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. 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?

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

${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 ' ? ', 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.

This works on 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:

#!/bin/sh

to

#!/bin/bash

and changing

echo "string data"

to

echo -e "string data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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