gpt4 book ai didi

linux - 在 GNU Bash 4.3 的 Bash shell 脚本中提示用户确认

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:05 25 4
gpt4 key购买 nike

我正在尝试创建一个 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

最佳答案

这是大小写修改替换。这是描述(来自 shell parameter expansion 上的 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 ‘?’, 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:]')

关于linux - 在 GNU Bash 4.3 的 Bash shell 脚本中提示用户确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com