gpt4 book ai didi

linux - 将用户输入转换为大写

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

我试图在 Unix 中创建一个程序来访问数据文件,在文件中添加、删除和搜索名称和用户名。通过这个 if 语句,我试图让用户通过第一个字段在文件中搜索数据。

文件中的所有数据都使用大写字母,因此我首先必须将用户输入的任何文本从小写字母转换为大写字母。出于某种原因,此代码不适用于转换为大写以及搜索和打印数据。

我该如何解决?

if [ "$choice" = "s" ] || [ "$choice" = "S" ]; then
tput cup 3 12
echo "Enter the first name of the user you would like to search for: "
tput cup 4 12; read search | tr '[a-z]' '[A-Z]'
echo "$search"
awk -F ":" '$1 == "$search" {print $3 " " $1 " " $2 }'
capstonedata.txt
fi

最佳答案

这:阅读搜索 | tr '[a-z]' '[A-Z]' 不会将任何内容分配给变量 search

应该是这样的

read input
search=$( echo "$input" | tr '[a-z]' '[A-Z]' )

最好对case modification使用参数扩展:

read input
search=${input^^}

关于linux - 将用户输入转换为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47718955/

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