- 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
Discussions
Discussions
Forums
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
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