Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for empty string in ksh

Tags:

ksh

I am writing a simple ksh script. I have to check if a string is empty or not. How to do so? This is what I have :

findRes=`find . -name believe | xargs grep -q "ser"`
if [ "${findRes:-unset}" != "unset" ];then

     print "${file}"
fi
like image 475
Programmer Avatar asked Jan 22 '14 04:01

Programmer


People also ask

How do I check if a string is empty in Bash?

n operator to check if string is empty. Most importantly, you should add spaces around the square brackets. If there are no spaces, bash will complain about that. Also, since any bash test without an argument will always return true, it is advisable to quote the variables so that the test will have at least an (empty) argument, ...

How do you check if a variable is empty or not?

Check to see if a variable is empty or not Create a new bash file, named, and enter the script below. The script above stores the first command-line argument in a variable and then tests the argument in the next statement. This script will print the first argument because it is not empty.

How to check if a string is null in bash script?

-n operator -n is one of the supported bash string comparison operators used for checking null strings in a bash script. When -n operator is used, it returns true for every case, but that’s if the string contains characters. On the other hand, if the string is empty, it won’t return true.

Can bash scripts have empty variables?

Sometimes, a bash script may contain empty variables. Here, we have provided every detail on how to check for empty variables in a bash script, as well as introduce a handy freeware for managing Linux partitions on Windows PC. Are you ready? Let's read! A variable is either defined or not defined.


1 Answers

empty is a string without char but defined, so in this case

[ -z "${MyVar}" ]

null is the case when your action failed, not when return empty reply also, grep -q could be tested with a [ $? -ne 0 ] when search does not find the pattern

like image 75
NeronLeVelu Avatar answered Sep 24 '22 02:09

NeronLeVelu