LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-10-2009, 10:51 AM   #1
jagooch
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Rep: Reputation: 1
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.

Thank You,


-TheGooch
 
Old 02-10-2009, 10:59 AM   #2
Maligree
Member
 
Registered: Mar 2008
Distribution: Gentoo, CentOS, Fedora, Arch
Posts: 231
Blog Entries: 1

Rep: Reputation: 42
Too many semicolons. Try it this way:
Code:
if [ 1 == 1 ]; then echo "true"; fi
 
Old 02-10-2009, 11:05 AM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by jagooch View Post
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

Last edited by rweaver; 02-10-2009 at 11:13 AM.
 
Old 02-10-2009, 06:16 PM   #4
custangro
Senior Member
 
Registered: Nov 2006
Location: California
Distribution: Fedora , CentOS , RHEL
Posts: 1,979
Blog Entries: 1

Rep: Reputation: 209Reputation: 209Reputation: 209
Quote:
Originally Posted by jagooch View Post
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

Last edited by custangro; 02-10-2009 at 06:18 PM.
 
Old 02-10-2009, 11:32 PM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by custangro View Post
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
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A single regex to match anything with ".aac" or ".mp3" at the end ? lumix Linux - General 9 05-09-2008 01:11 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
fdisk reports odd "Start "and "End" sectors on single partition eponymous Linux - Software 3 10-01-2007 03:41 PM
What is wrong in the statement: $line || echo "error" && break; nadavvin Linux - General 1 08-05-2006 05:25 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 11:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration