- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: variable in if
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 02:50 PM
04-14-2008 02:50 PM
"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
Solved! Go to Solution.
- Tags:
- command substitution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 03:42 PM
04-14-2008 03:42 PM
Re: variable in if
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 03:44 PM
04-14-2008 03:44 PM
Re: variable in if
I just want to condense two statements into one in the if statement. can that be done?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 03:57 PM
04-14-2008 03:57 PM
Re: variable in if
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 04:01 PM
04-14-2008 04:01 PM
Re: variable in if
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 04:12 PM
04-14-2008 04:12 PM
Re: variable in if
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 04:22 PM
04-14-2008 04:22 PM
Re: variable in if
[ -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..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 05:28 PM
04-14-2008 05:28 PM
Re: variable in if
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2008 05:34 PM
04-14-2008 05:34 PM
Re: variable in if
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 12:23 AM - edited 09-16-2011 07:01 PM
04-15-2008 12:23 AM - edited 09-16-2011 07:01 PM
Solution>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.
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 01:59 PM
04-15-2008 01:59 PM
Re: variable in if
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 "="
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 02:59 PM
04-15-2008 02:59 PM
Re: variable in if
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 03:20 PM
04-15-2008 03:20 PM
Re: variable in if
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}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 03:26 PM - edited 09-16-2011 07:03 PM
04-15-2008 03:26 PM - edited 09-16-2011 07:03 PM
Re: variable in if
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}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2008 03:42 PM
04-15-2008 03:42 PM
Re: variable in if
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...