[英]Is it possible to compare 2 contents of same line in .txt file using linux?
Let's say I have the following content in text file假设我在文本文件中有以下内容
input.txt
aaa bbb
ccc ddd
I would like to compare user input with the content in text file.我想将用户输入与文本文件中的内容进行比较。
Let's say user is prompted to enter 2 strings, if first string matches the content in text file (let's say aaa, which is the first content of line 1) , how should I modify my code to make the program to compare the second string that user entered with the second content (bbb) on the same line?假设提示用户输入 2 个字符串,如果第一个字符串与文本文件中的内容匹配(比如说 aaa,这是第 1 行的第一个内容) ,我应该如何修改我的代码以使程序比较第二个字符串用户在同一行输入了第二个内容(bbb) ?
echo "First input: "; read input1
echo "Second input: "; read input2
if [ "$input1" = $(< input.txt) ]; then
# content matches
else
# content doesn't match
fi
Read the first line from input.txt
:从
input.txt
中读取第一行:
read -r shouldbe_input1 shouldbe_input2 < <(head -n1 input.txt)
Then just compare:然后只是比较:
if [ "$input1" = "$shouldbe_input1" ] && [ "$input2" = "$shouldbe_input2" ]; then
echo yay
else
echo not yay
fi菲
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.