Shell-script - Find two string are not empty, Compare two variables are not empty

Find two string are not empty

While writing shell script, we might be willing to check the argument passed by us it a valid string or not. In order to check the valid string usinf "and" logical operatior we can use below method to check the variables are non empty.

#!/bin/bash
echo "Enter FirstName"
read first
echo "Enter Second Name"
read second

if [ $first ] && [ $second ]
then echo "Both $first $second are valid"
else echo "Fill both variables"
fi

The same scrip can be eiter written as below
#!/bin/bash
echo "Enter FirstName"
read first
echo "Enter Second Name"
read second

if [[ -n "$first" && -n "$second" ]]
 then echo "Both variables are valid"
 else echo "Fill both variables"
fi

Usefull tip while using logical operator
if [ $var1 -eq 0 -a $var2 -eq 0 ]; then
if [ $var1 -eq 0 ] && [ $var2 -eq 0 ]; then
if (( var1 % 2 == 0 )) && (( var2 % 2 == 0 )); then
if (( var1 % 2 == 0 && var2 % 2 == 0 )); then

More info can be found in test command.
 # man test

The topic on Shell-script - Find two string are not empty is posted by - Math

Hope you have enjoyed, Shell-script - Find two string are not emptyThanks for your time

Tech Bluff