Re: variable in if (221 Views)
Reply
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 1 of 15 (221 Views)
Accepted Solution

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
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 15 (221 Views)

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...
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 3 of 15 (221 Views)

Re: variable in if

not quite.

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

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 15 (221 Views)

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...
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 5 of 15 (221 Views)

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





Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 15 (221 Views)

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...
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 7 of 15 (221 Views)

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..
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,084
Registered: ‎02-23-2005
Message 8 of 15 (221 Views)

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?
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 9 of 15 (221 Views)

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
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,975
Registered: ‎03-06-2006
Message 10 of 15 (221 Views)

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.

Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 11 of 15 (221 Views)

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 "="
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 12 of 15 (221 Views)

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...
Please use plain text.
Regular Advisor
Gemini_2
Posts: 188
Registered: ‎11-05-2001
Message 13 of 15 (221 Views)

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}
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,975
Registered: ‎03-06-2006
Message 14 of 15 (221 Views)

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}

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 15 of 15 (221 Views)

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...



Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation