簡體   English   中英

是否可以使用 linux 比較同一行 in.txt 文件的 2 個內容?

[英]Is it possible to compare 2 contents of same line in .txt file using linux?

假設我在文本文件中有以下內容

input.txt
aaa bbb
ccc ddd

我想將用戶輸入與文本文件中的內容進行比較。

假設提示用戶輸入 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

input.txt中讀取第一行:

read -r shouldbe_input1 shouldbe_input2 < <(head -n1 input.txt)

然后只是比較:

if [ "$input1" = "$shouldbe_input1" ] && [ "$input2" = "$shouldbe_input2" ]; then
       echo yay
else
       echo not yay

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM