How Can I Use An "if" statement on a Single Command Line?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How Can I Use An "if" statement on a Single Command Line?
I would like to execute some branching logic on a single command line.
So, I want to say something like:
if [ -z "`ls -A /home/autosys/archive`" ]; then; rm /home/autosys/archive/*.*;fi;
Except it doesn't work. It give me an "invalid token near 'then'" message when I run it. I've played with the semicolon near 'then' a bit and cannot get it to work. I've removed it , place it before and after the 'then' without success.
I would like to execute some branching logic on a single command line.
So, I want to say something like:
if [ -z "`ls -A /home/autosys/archive`" ]; then; rm /home/autosys/archive/*.*;fi;
Except it doesn't work. It give me an "invalid token near 'then'" message when I run it. I've played with the semicolon near 'then' a bit and cannot get it to work. I've removed it , place it before and after the 'then' without success.
Thank You,
-TheGooch
Basically you have some extra ;'s where they're not needed. Remove teh one after the "then" and remove the one after the "fi" and the rest should work provided its correct (I don't think that if statement does what you expect.)
Basically, each element that *requires* a line after it must have a ; otherwise it shouldn't. These are all valid as far as structure go.
Code:
if [ -z "`ls -A /home/autosys/archive`" ]; then rm /home/autosys/archive/*.*; fi
Code:
if [ -z "`ls -A /home/autosys/archive`" ]
then
rm /home/autosys/archive/*.*
fi
Code:
if [ -z "`ls -A /home/autosys/archive`" ]
then rm /home/autosys/archive/*.*
fi
Code:
if [ -z "`ls -A /home/autosys/archive`" ]; then
rm /home/autosys/archive/*.*
fi
I would like to execute some branching logic on a single command line.
So, I want to say something like:
if [ -z "`ls -A /home/autosys/archive`" ]; then; rm /home/autosys/archive/*.*;fi;
Except it doesn't work. It give me an "invalid token near 'then'" message when I run it. I've played with the semicolon near 'then' a bit and cannot get it to work. I've removed it , place it before and after the 'then' without success.
Thank You,
-TheGooch
Or you can...
Code:
root@host# [ -z $(ls -A /home/autosys/archive) ] && rm /home/autosys/archive/*.*
FYI...using `command` is deprecated, but if you're in a shell it's better because it's faster to type
root@host# [ -z $(ls -A /home/autosys/archive) ] && rm /home/autosys/archive/*.*
FYI...using `command` is deprecated, but if you're in a shell it's better because it's faster to type
Assuming that "archive" is a dir, which I deduct from the thread, this would suffice:
Code:
[ -a /home/autosys/archive/* ] && rm -rf /home/autosys/archive/*
# or
if [ -a /home/autosys/archive/* ]; then rm -rf /home/autosys/archive/*; fi
There's no need to throw "ls" in the middle if you are using bash. Also, check the bash man page (CONDITIONAL EXPRESSIONS) if you want to know more about this stuff. -a will check that the file exists, but maybe you want to check for -f (regular files only) and change rm -rf by rm -f... It all depends on what do you exactly need.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.