Find the answer to your Linux question:
Results 1 to 4 of 4
Hello. Im new to shell scripting and am looking for any advice on the following code. The script performs the following: 1) downloads files into 2 locations from ftp server ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    9

    New to scripting. looking for critiques / advice

    Hello. Im new to shell scripting and am looking for any advice on the following code. The script performs the following:

    1) downloads files into 2 locations from ftp server

    2) verifies file exists in $app location (an application processes the file from there) before deleting it from the ftp

    3) checks a local folder for a file, if file exists, it uploads it to the ftp

    Any advice or critiquing is welcome!

    Code:
    # Copies down the files in two locations -
    # App directory and tmporary directory for redunadancy.
    
    ftp -n -i $1 <<EOF
    user $USERNAME $PASSWORD
    cd /Export
    lcd $temp_dir
    mget *.txt
    lcd $app_dir
    mget *.txt
    bye
    EOF
    
    
    # Verifies File exists in app directory then deletes
    # temp file and deletes from remote server
    
    
    find $temp_dir/ -iname "*txt" -printf %f\\n | \
    while read I; do
    if [ -f "$app_dir"/"$I" ]; then
            echo ""$I" Allready Exists In Star Folder. Removing Temp File... "
            rm -f $temp_dir/"$I"
            echo "Deleting File From Remote Host.."
            ftp -n -i $1 <<EOF
            user $USERNAME $PASSWORD
            cd /Export
            mdelete *.txt
            bye
    EOF
    else
            echo "$I Does Not Exist. Copying File to Star Data Folder..."
            cp $temp_dir/"$I" $app_dir
    fi
    done;
    
    
    
    # Checks for updates in local /Updates folder and sends to remote machine
    # (Still needs to be based on machine name)
    
    updates_dir="/updates"
    
    if [ "$(ls -A $updates_dir)" ]; then
            echo "Update file found! Sending to remote host."
            ftp -n -i $1 <<EOF
            user $USERNAME $PASSWORD
            cd Updates
            mput $updates_dir/*
            quit
    EOF
    else
            echo "No updates to install"
    fi

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    Just my thought
    rm -f $temp_dir/"$I"
    Using rm -f is always a risky affair. So i would avoid applying rm on variable.
    what if the variable is empty?
    So I would suggest you to move to trash directory and then delete it.

    mkdir ~/trash
    mv $temp_dir/"$I" ~/trash
    rm -rf ~/trash #we are not using any variable but exact path
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Just Joined!
    Join Date
    Sep 2008
    Posts
    3
    for security reasons you should put the username and password for the ftp server in ~/.netrc

  4. #4
    Linux Newbie unlimitedscolobb's Avatar
    Join Date
    Jan 2008
    Posts
    120
    Quote Originally Posted by marafa View Post
    for security reasons you should put the username and password for the ftp server in ~/.netrc
    And then chmod 0600 ~/.netrc so that nobody can see it.

    Also, doing something like:
    Code:
    ...
    find $temp_dir/ -iname "*txt" -printf %f\\n | \
    ...
    risks getting a successful match against files like hellotxt (note the absence of the dot). While not critical in itself, it makes the constraints looser and thus is a potential source of problems. I might be paranoid, but I'm trying to keep the tradition of specifying the dot

Posting Permissions

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