简体   繁体   中英

Comparing Strings in Arrays in Bash Scripting

Alright, this is a really simple little thing but it's gotten to the point where I've exhausted 2-3 hours trying to find out why this isn't working and have gotten nowhere.

I'm trying to make Bash/Shell-Script program, and at one point in it, I want to check the contents of an array of strings to see if the string is blank. My code is as follows:

#Display any words the user corrected, and what he/she corrected it with (ignore blank corrections)
printf "\n\n" "MISPELLED  /  CORRECTIONS"
for (( i=0; i<${#words[*]}; i++)); do
if [!"${corrections[$i]}"=""]; then    //this is line 25
   printf "\n ${words[$i]}   ${corrections[$i]}"
fi
done

I'm not positive if the way I used the ! operator was legal, but with or without it, I get the run-time error:

./test: line 25: [=]: command not found

I can post the rest of the code if need be, though I'm mostly confident that the "corrections" array is properly filled with strings.

In the shell, [ is a command, so it is important to leave spaces before and after it. On top of that, if you want to check that a string is not empty, you can use -n :

if [ -n "${corrections[$i]}" ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM