我有我想設置這樣的一個變量:爲什麼shell不會設置一個管道變量?
#!/bin/sh
found=0
id=1
echo "Hello" |
while [ $id != 5 ]
do
id=`expr $id + 1`
echo $id
found=1
done
echo "found = $found" // I expect this to be 1
爲什麼,以及如何設置這個值? 我不得不使用這樣(管道),因爲在生產環境中的實際代碼是:
found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" | mysql ifm -uroot -pinsite3 |
while read file_system_id mount_name
do
if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
echo "Match found for file system ID and mount name"
found=1
fi
done
echo "found = $found" // I expect this to be 1, when a match, but does not
這是因爲變量是在子shell設置。爲了使它在同一個shell中工作,請測試其他方法。因爲事實上,爲什麼在'echo'後面輸入? – fedorqui
當您運行它時,您是否看到消息「找到匹配的文件系統ID和裝載名稱」? –
剛纔,我得到了一個可行的答案,但它被刪除了。事實上,它解決了這個問題。 – kingsmasher1