Results 1 to 5 of 5

Thread: if/then Loop question

  1. #1

    if/then Loop question

    Am I missing something in this example? This isn't my actual script, but is a simplified script so I can test whether an idea will work or not. If it works, I'll use it, but right now, it doesn't work, and I can't for the life of me, figure out why:

    ------------------------------------------------------------------------------------

    #!/bin/bash
    clear

    LOGDIR=/usr/local/bin/test

    if [ -d $LOGDIR ]
    then echo "Client-side backup log directory \"$LOGDIR\" found."
    else echo "Client-side backup log main directory not found. Creating this directory now..."
    mkdir $LOGDIR
    if [ -d $LOGDIR ]
    then echo "Backup log main directory created successfully."
    else echo "There was an error creating the main directory for the client-side backup log files. Please check your permissions in the logfile destination, and ensure that there is free space to create the neccessary files. Do you want to continue the backup process without the log files? ('y'es or 'n'o)"
    read $LOGANS
    if [ $LOGANS = "y" ]
    then echo "Continuing the backup process.."
    else echo "You choose to exit..."
    exit
    fi
    fi
    fi
    echo "ctrl+c to exit..."
    sleep 20

    ---------------------------------------------------------------------------------------------------------

    Now, if the directory exists, everything works. If the directory doesn't exist, but I have permissions to create it, everything works. My problem is where, if you DO NOT have permissions, and it asks if you want to continue or not, the script disregards my answer. If I answer 'n', it still says 'Continuing the backup process.. ctrl+c to exit...' but it also gives me an error for "./loop.test.sh: line 17: [: !=: unary operator expected". Now, I messed around with different quotes, putting the quotes in different places, and didn't make much progress. I know there may be a better way to do this (I read some about case statements), so maybe I will try that. I would however, at least like to know how the if/then works, and why this example doesn't.
    As always, thanks for the help/insight!
    Asus A8V Deluxe Mobo
    AMD Athlon64 X2 4400 -939
    2GB Crucial DDR3200 Dual-Channel
    ATi AiW 9800 Pro 128DDR
    Audigy2 ZS
    Sony DRU-810 DVD+RW DL
    3 x Maxtor MaxLine SATA 160Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 300Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 120Gb 16Mb Cache

  2. #2
    Okay, so through some reading, it seems that when I did the 'if [ $LOGANS...', I should have put quotes around the variable name. My sources said that if there is any chance that the variable will be empty (in this case, if someone just hit ENTER instead of typing in a key, THEN ENTER) then you need to quote it to get rid of a syntax error. So, I did this. Now, I do NOT get the error, but no matter if I enter 'y' or 'n', I still get the message that I chose to abort...
    Asus A8V Deluxe Mobo
    AMD Athlon64 X2 4400 -939
    2GB Crucial DDR3200 Dual-Channel
    ATi AiW 9800 Pro 128DDR
    Audigy2 ZS
    Sony DRU-810 DVD+RW DL
    3 x Maxtor MaxLine SATA 160Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 300Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 120Gb 16Mb Cache

  3. #3
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    A small change:
    Code:
    ...
    read LOGANS
    if [ "$LOGANS" = "y" ]; then
        echo "Continuing the backup process.." 
    else
        echo "You choose to exit..."
        exit
    fi
    ...
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

  4. #4
    You know, I read your reply, but the only thing I noticed off the bat was the ";" placed after the 'if' statement. So, I did that and my results were the same. Then I fire up the script in vi, and noticed the 'read $LOGANS' part, and realized that I needed to drop the $ part, as it would lead the script to read something that hasn't been created yet. Now, all is well. After this, I felt stupid and proud at the same time, until I came online to post this reply, and it hit me, that you had already pointed that out to me in your reply! I am just an observant one this morning...
    In any case, you are the man! Thanks for being patient with this noob...
    Asus A8V Deluxe Mobo
    AMD Athlon64 X2 4400 -939
    2GB Crucial DDR3200 Dual-Channel
    ATi AiW 9800 Pro 128DDR
    Audigy2 ZS
    Sony DRU-810 DVD+RW DL
    3 x Maxtor MaxLine SATA 160Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 300Gb 16Mb Cache
    1 x Maxtor MaxLine SATA 120Gb 16Mb Cache

  5. #5
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    The added ; was just because I dont want to add a newline between if[] and then, I like them to be combined on the same line, it's a bad habit from programming too much Moscow ML, the real deal here, was the removal of $ aswell as adding quotes around $LOGANS in the if clause, to avoid NULL values, but you already mentioned that earlier.
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

Similar Threads

  1. loop with input file?
    By sabre in forum Linux - Software, Applications & Programming
    Replies: 9
    Last Post: 06-22-2010, 10:48 PM
  2. restarting loop
    By tolentinopol in forum Windows - General Topics
    Replies: 1
    Last Post: 01-10-2007, 10:07 AM
  3. rebooting loop
    By j9sheldon in forum Windows - General Topics
    Replies: 1
    Last Post: 10-24-2006, 05:38 AM
  4. compile stuck in loop?
    By cj171 in forum Linux - General Topics
    Replies: 8
    Last Post: 05-08-2004, 10:10 PM
  5. Loop Back Encrypted Filesystem
    By stryder144 in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 01-14-2004, 09:28 PM

Bookmarks

Posting Permissions

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