I have a file in which value starts with ABC or ABD.
After fetching the value from input file how to check it starts with ABC or ABD?
eg:
value=$(cat file | awk 'NF{print;exit}' | awk '{print $1}')
and now I want to check this $value is starting with ABC or ABD . how?
Best Answer
Using
case
To check if a shell variable starts with ABC or ABD, a traditional (and very portable) method is to use a case statement:
Because this requires no external processes, it should be fast.
Using
[
Alternatively, one can use a test command:
This is also quite portable.
Using
[[
Lastly, one can use the more modern and less portable test command:
Reading the file and testing all in one step
To read the first nonempty line of a file and test if its first field start with ABC or ABD: