2016-07-07 62 views
1

我試圖創建一個shell腳本來爲Laravel應用程序設置我的Ubuntu服務器。用戶被要求從這裏採取了以下代碼繼續之前確認:用於GNU Bash的Bash shell腳本中的提示用戶配置4.3

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 
+0

你是如何運行腳本?你的代碼很好。什麼操作系統(你說Linux,但這是在一些windoze解釋器下?)測試在'GNU bash,版本4.3.42(1) - 發佈' –

+0

我運行腳本通過調用./script-name後我chmod u + x,在Ubuntu 16.04上運行GNU bash,版本4.3.42(1)-release(x86_64-pc-linux-gnu) –

+0

我會檢查以確保你沒有「回車」或其他一些雜項* Unicode *字符嵌入在腳本中。實際的腳本是確定的。用'hexdump -C yourscriptname'確認。另外,在你的實際腳本中,你在頂部包括'#!/ bin/bash',對嗎? (我不知道Ubuntu使用什麼默認shell) –

回答

0

感謝David C. Rankins幫助這個問題,通過改變解析:

#!/bin/sh 

#!/bin/bash 

和改變

echo "string data" 

echo -e "string data" 
0

這是一個案例修改替換。下面是(上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.

這適用於的bash> = 4.0。

或者,也可以在評論部分使用

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

也許他的'#tolower'評論是他明白的一個贈品? –