Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
  1. #1
    Just Joined!
    Join Date
    Sep 2008
    Posts
    2

    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.

  2. #2
    Just Joined!
    Join Date
    Sep 2008
    Posts
    2
    Question was answered on another forum, reposted here to help anyone else who has the same problem:

    I needed spaces either side of my condition:
    Code:
    if [ "$1" != "a" ]; then
    now it works... Painfully simply but elusive...!

  3. #3
    Linux Guru smolloy's Avatar
    Join Date
    Apr 2005
    Location
    CA, but from N.Ireland
    Posts
    2,413
    Glad you got it fixed -- it was torturing me!!!

    Welcome to the forums!
    Registered Linux user #388328 || Registered LFS user #15880
    AMD 64 X2 4600+ :: 2X1GB DDR2 800 :: GeForce 9400 GT 512MB :: ASUS M2N32 Deluxe :: 4X250GB SATAII
    Need instant help? Try us on IRC -- #linuxforums on freenode

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...