![](/img/trans.png)
[英]How to concatenate strings obtained from a .txt document with BASH script?
[英]How concatenate strings and display that formated string in linux bash script?
我寫下面的腳本
#!/bin/bash
str1="apple"
str2="banana"
str3="NA"
str4="lumia"
str5="nokia"
let maxarg=5
checkindex()
{
while [ $maxarg -gt 0 ]
do
str="${str$maxarg}" //here is problem
echo -e $str
if [ "${str}" == "NA" ]
then
break
fi
((maxarg--))
done
printf "Index is %d\n" $maxarg
}
checkindex
當exicuting它我得到str1,str2....Index is 0
輸出但我想打印apple,banana....Index is 3
意味着捕獲NA
字符串找到的索引。 使用str="${str$maxarg}"
我試圖在str
重定向str1,str2....str5
的輸出,因為我不打算使用任何switch case
或if..else
來比較每個字符串。 有幫助嗎?
您需要參考間接擴展 。 替換線
str="${str$maxarg}" //here is problem
同
tmp="str${maxarg}" # This sets the variable tmp to str1 and so on
str=${!tmp} # This performs indirect expansion to retrieve
# the value of the variable name stored in tmp
應該讓它發揮作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.