LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-30-2008, 03:53 PM   #1
Critcho
LQ Newbie
 
Registered: Feb 2006
Posts: 20

Rep: Reputation: 0
Conditional (IF) test in bash sh script - presence of first parameter


I have devices that I have to FTP to and copy log files from. The log files have 3 generations, that typically roll over every few days. i.e. if I pulled the files down yesterday, I dont want all 3 generations; if it's been a while, I do.

So I have made a script, I expected to be able to pass it a parameter to decide which files to bring down, but it isn't working.

e.g. ". GetLogFiles.sh" will get just the latest generation, ". GetLogFiles.sh a" will get all.

My script looks something like
Code:
#!/bin/sh

HOST='1.2.3.4'
USER='user1'
PASS='password'
FTP='/usr/bin/ftp'
Today="`date +%y%m%d`"

        CMD1='get //var//log//messages SiteName_'$Today'.messages.log'
        CMD2='get //var//log//messages.0 SiteName_'$Today'.messages.0.log'
        CMD3='get //var//log//messages.1 SiteName_'$Today'.messages.1.log'

        if [ "$1"!="a" ]; then
                echo "Only copying messages, not messages.0 or messages.1"
                CMD2=''
                CMD3=''
        fi

$FTP -n -v $HOST << END_S

quote USER $USER
quote PASS $PASS

hash

$CMD1
$CMD2
$CMD3

END_S
I tired a few variants of
Code:
    if [ "$1"!="a" ]; then
    if [ $1!="a" ]; then
    if ![ $1=="a" ]; then
etc to no avail. At the moment it grabs all files.

Any clues?
Thanks!
Critcho.

Last edited by Critcho; 09-30-2008 at 04:27 PM.
 
Old 09-30-2008, 03:57 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

Try:

if [ "$1" -ne "a" ]

Dave
 
Old 09-30-2008, 04:46 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Or, since $1 may be null, if [ x$1 -ne xa ], although quoting (as ilikejam suggested) should work quite well.
 
Old 09-30-2008, 05:22 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
-ne is a numeric test but $1 seems to be a string variable.
The string test != needs a space on both sides of it. Try:-
Code:
if [ "$1" != "a" ]; then
 
Old 09-30-2008, 05:28 PM   #5
Critcho
LQ Newbie
 
Registered: Feb 2006
Posts: 20

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by Kenhelm View Post
The string test != needs a space on both sides of it. Try:-
Code:
if [ "$1" != "a" ]; then
Cool, that did it! Knew it was something painfully simple....

THANKS!!!!
 
Old 09-30-2008, 05:36 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Quote:
Originally Posted by Kenhelm View Post
-ne is a numeric test
Oh yeah. So it is. Back to school for me I think.
 
Old 10-01-2008, 12:20 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
an alternate to this

CMD1='get //var//log//messages SiteName_'$Today'.messages.log'

is


CMD1="get //var//log//messages SiteName_${Today}.messages.log"
 
  


Reply

Tags
bash, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help with Bash script (test) Benanzo Linux - Software 1 04-09-2007 03:18 AM
Test output from bash script estratos Programming 6 11-16-2006 09:01 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
bash script 'test' question linmix Programming 5 11-29-2005 11:49 AM
benchmark test bash script PTBmilo Programming 3 02-14-2003 04:37 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration