BladeSystem Forums have moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

variable in if

SOLVED
Go to Solution
Regular Advisor

variable in if

I would like to change the following two statements into one.

"list=$(ls -1 $xturns/tur*)
if [ "$list" ];then"

how can I do that?

i tried
if [ $list=$(ls -1 $xturns/tur*) ]; then

but...it doesnt work...

for shell script..please advise
14 REPLIES
Acclaimed Contributor

Re: variable in if

Hi Gemini:

# LIST=$(ls -1 /tmp/tur* 2>/dev/null||echo "empty")

# echo ${LIST}

...would return a list of files or the word "empty"

Is that your objective?

Regards!

...JRF...
Regular Advisor

Re: variable in if

not quite.

I just want to condense two statements into one in the if statement. can that be done?

Acclaimed Contributor

Re: variable in if

Hi:

Again, I'm not sure of your objective, but:

if [ -z "$(ls -1 /tmp/gemini* 2>/dev/null)" ]; then
echo "empty"
else
echo "ok"
fi

...Note that no variable is necessary for capturing the list.

Regards!

...JRF...
Regular Advisor

Re: variable in if

ah, you are right.. I didnt make myself clear. I left one important part. my bad.

use your example

if [ -z "$(ls -1 /tmp/gemini* 2>/dev/null)" ]; then
echo "empty"
else
echo "ok"
fi

what if i want to echo the result of ls -l? then what do you do

thanks for your patience





Acclaimed Contributor

Re: variable in if

Hi (again):

I think the best we might do is:

# LIST=$(ls -1 /tmp/gemini* 2>/dev/null)
# [ -z "${LIST}" ] && echo "empty" || echo ${LIST}

...yes, that's two statements, but ...

Regards!

...JRF...
Regular Advisor

Re: variable in if

I was hoping to have something like this

[ -z "${LIST}=$(ls -1 /tmp/gemini* 2>/dev/null)" ] && echo "empty" || echo ${LIST}


but, i know my syntax is not quite right...


anyway..thaks for your help..
Honored Contributor

Re: variable in if

Just so I understand, ...

You have two simple statements which work
properly, and are easy to understand, and you
wish to combine them into a single, more
complex statement because you think that that
will look better somehow?

Is that the goal here?
Regular Advisor

Re: variable in if

I know

I just thought that option is available...but I forgot it...I just want to know it for memory refresh purple

but, thanks for your help anyway
Acclaimed Contributor

Re: variable in if

[ Edited ]

>I would like to change the following two statements into one.

You would do the obvious:
if [ "$(ls -1 $xturns/tur*)" ]; then

Compare to JRF's solution where -z tests for empty string.

>what if I want to echo the result of ls -l?

As Steven said, why make it harder to understand?

But if you insist, if may be something like:
[ -z "${LIST:=$(ls -1 /tmp/gemini* 2>/dev/null)}" ] && echo "empty" || echo ${LIST}

Note: You would really need to unset LIST to make sure it gets set.

Regular Advisor

Re: variable in if

yes, yes, your answer is correct.

it was very closed to what I had, but I placed my quote place wrong.

thanks for your help!

ps: you also dont need the ":=", you only need "="
Acclaimed Contributor

Re: variable in if

Hi (again) Gemini:

I too had concocted a solution identical to by Dennis'. It would seem to be correct (using '=' instead of the ALGOL assignment ':=' operator) and yet failed/fails for me:

# [ -z "${LIST=$(ls -1 /tmp/gemini* 2>/dev/null})" ] && echo "empty" || echo ${LIST}

This does not yield any results for me; rather it gives and empty list. Comments, gentlemen?

Regards!

...JRF...
Regular Advisor

Re: variable in if

oh, he had a little typo in his statement..
I corrected in my script, but I didnt point it out.

this should be right.

[ -z "${LIST=$(ls -1 /tmp/gemini* 2>/dev/null)}" ] && echo "empty" || echo ${LIST}
Acclaimed Contributor

Re: variable in if

[ Edited ]

I made a typo, swap the "}" and ")":
[ -z "${LIST:=$(ls -1 /tmp/gemini* 2>/dev/null)}" ]

>you also don't need the ":=", you only need "="

Yes, only because $LIST must NOT be set at all.

>JRF: This does not yield any results for me; rather it gives and empty list. Comments, gentlemen?

Did you do this as root? You have created a file in /dev: null}

Highlighted
Acclaimed Contributor

Re: variable in if

Hi Gemini & Dennis:

Aahhaa! Yes, OK.

> Dennis: I made a typo, swap the "}" and ")":

Oh, OK, I kept missing that :-{

> Dennis: Did you do this as root? You have created a file in /dev: null}

And, again, yes! I did and I had a file in '/dev/' so named.

Very nice solution, Dennis.

Regards!

...JRF...