LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to test variables in script? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-test-variables-in-script-4175453093/)

Batistuta_g_2000 03-07-2013 06:09 AM

How to test variables in script?
 
Hi,

Is this doing what i think, I need to make a dir or file if it doesn't exist - have written the below but not sure if it is working or overwriting dir each time?

Code:

function setup_dirs {
    [ -d $IMPORTANT_FILES_DIRECTORY ] || mkdir -p $IMPORTANT_FILES_DIRECTORY
    [ -d $BACKUP_FILES_DIRECTORY ] || mkdir -p $BACKUP_FILES_DIRECTORY
    [ -d $RESTORE_FILES_DIRECTORY ] || mkdir -p $RESTORE_FILES_DIRECTORY
    [ -d ~/assignment1/tmp/ ] || mkdir ~/assignment1/tmp/                             
    [ -d ~/assignment1/tmp/backuplist.txt ] || touch ~/assignment1/tmp/backuplist.txt


shivaa 03-07-2013 06:52 AM

Test will work if it's passed to asisgnment. Like you can try:
function setup_dirs {
Code:

    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then
        mkdir -p $IMPORTANT_FILES_DIRECTORY
    fi

OR, combine more conditions, as:
Code:

if ([ -d $IMPORTANT_FILES_DIRECTORY ]; [ -d $BACKUP_FILES_DIRECTORY ]; [ -d $RESTORE_FILES_DIRECTORY ]; [ -d ~/assignment1/tmp/ ]; [ -f ~/assignment1/tmp/backuplist.txt ]); then
mkdir $IMPORTANT_FILES_DIRECTORY $BACKUP_FILES_DIRECTORY $RESTORE_FILES_DIRECTORY ~/assignment1/tmp/
touch ~/assignment1/tmp/backuplist.txt
fi

Note: In order to test a file, use -f option. Also once go through manual of test cmd here.

Batistuta_g_2000 03-07-2013 08:03 AM

Quote:

Originally Posted by shivaa (Post 4906641)
Test will work if it's passed to asisgnment. Like you can try:
function setup_dirs {
Code:

    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then
        mkdir -p $IMPORTANT_FILES_DIRECTORY
    fi

OR, combine more conditions, as:
Code:

if ([ -d $IMPORTANT_FILES_DIRECTORY ]; [ -d $BACKUP_FILES_DIRECTORY ]; [ -d $RESTORE_FILES_DIRECTORY ]; [ -d ~/assignment1/tmp/ ]; [ -f ~/assignment1/tmp/backuplist.txt ]); then
mkdir $IMPORTANT_FILES_DIRECTORY $BACKUP_FILES_DIRECTORY $RESTORE_FILES_DIRECTORY ~/assignment1/tmp/
touch ~/assignment1/tmp/backuplist.txt
fi

Note: In order to test a file, use -f option. Also once go through manual of test cmd here.


Thanks, changed it to below but getting error somewhere, cant figure it out,
does mkdir have to be on next line?

Code:

function setup_dirs {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then mkdir -p $IMPORTANT_FILES_DIRECTORY fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then mkdir -p $BACKUP_FILES_DIRECTORY fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then mkdir -p $RESTORE_FILES_DIRECTORY fi
    if [ -d ~/assignment1/tmp/ ]; then mkdir ~/assignment1/tmp/ fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then touch ~/assignment1/tmp/backuplist.txt fi
}


shivaa 03-07-2013 08:09 AM

Use a semicolon before fi. Or try:
Code:

function setup_dirs {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then
    mkdir -p $IMPORTANT_FILES_DIRECTORY
    fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then
    mkdir -p $BACKUP_FILES_DIRECTORY
    fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then
    mkdir -p $RESTORE_FILES_DIRECTORY
    fi
    if [ -d ~/assignment1/tmp/ ]; then
    mkdir ~/assignment1/tmp/
    fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then
    touch ~/assignment1/tmp/backuplist.txt
    fi
}

OR
Code:

function setup_dirs {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then mkdir -p $IMPORTANT_FILES_DIRECTORY; fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then mkdir -p $BACKUP_FILES_DIRECTORY; fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then mkdir -p $RESTORE_FILES_DIRECTORY; fi
    if [ -d ~/assignment1/tmp/ ]; then mkdir ~/assignment1/tmp; fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then touch ~/assignment1/tmp/backuplist.txt; fi
}


Batistuta_g_2000 03-07-2013 08:11 AM

Quote:

Originally Posted by Batistuta_g_2000 (Post 4906689)
Thanks, changed it to below but getting error somewhere, cant figure it out,
does mkdir have to be on next line?

Code:

function setup_dirs {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then mkdir -p $IMPORTANT_FILES_DIRECTORY fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then mkdir -p $BACKUP_FILES_DIRECTORY fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then mkdir -p $RESTORE_FILES_DIRECTORY fi
    if [ -d ~/assignment1/tmp/ ]; then mkdir ~/assignment1/tmp/ fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then touch ~/assignment1/tmp/backuplist.txt fi
}



Think I got it - Thanks

Code:

#!/bin/bash
function setup_dirs {
    if [ ! -d $IMPORTANT_FILES_DIRECTORY ]; then
mkdir -p $IMPORTANT_FILES_DIRECTORY
fi
    if [ ! -d $BACKUP_FILES_DIRECTORY ]; then
mkdir -p $BACKUP_FILES_DIRECTORY
fi
    if [ ! -d $RESTORE_FILES_DIRECTORY ];
then mkdir -p $RESTORE_FILES_DIRECTORY
fi
    if [ ! -d ~/assignment1/tmp/ ];
then mkdir ~/assignment1/tmp/
fi
    if [ ! -f ~/assignment1/tmp/backuplist.txt ]; then
touch ~/assignment1/tmp/backuplist.txt
fi
}
setup_dirs


shivaa 03-07-2013 08:15 AM

Also, one important thing to notice that you've defined function incorrectly. Use () after function name while defining it, as:
Code:

function setup_dirs() {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then
    mkdir -p $IMPORTANT_FILES_DIRECTORY
    fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then
    mkdir -p $BACKUP_FILES_DIRECTORY
    fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then
    mkdir -p $RESTORE_FILES_DIRECTORY
    fi
    if [ -d ~/assignment1/tmp/ ]; then
    mkdir ~/assignment1/tmp/
    fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then
    touch ~/assignment1/tmp/backuplist.txt
    fi
}

OR
Code:

function setup_dirs() {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then mkdir -p $IMPORTANT_FILES_DIRECTORY; fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then mkdir -p $BACKUP_FILES_DIRECTORY; fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then mkdir -p $RESTORE_FILES_DIRECTORY; fi
    if [ -d ~/assignment1/tmp/ ]; then mkdir ~/assignment1/tmp; fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then touch ~/assignment1/tmp/backuplist.txt; fi
}


Batistuta_g_2000 03-07-2013 08:32 AM

Quote:

Originally Posted by shivaa (Post 4906703)
Also, one important thing to notice that you've defined function incorrectly. Use () after function name while defining it, as:
Code:

function setup_dirs() {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then
    mkdir -p $IMPORTANT_FILES_DIRECTORY
    fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then
    mkdir -p $BACKUP_FILES_DIRECTORY
    fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then
    mkdir -p $RESTORE_FILES_DIRECTORY
    fi
    if [ -d ~/assignment1/tmp/ ]; then
    mkdir ~/assignment1/tmp/
    fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then
    touch ~/assignment1/tmp/backuplist.txt
    fi
}

OR
Code:

function setup_dirs() {
    if [ -d $IMPORTANT_FILES_DIRECTORY ]; then mkdir -p $IMPORTANT_FILES_DIRECTORY; fi
    if [ -d $BACKUP_FILES_DIRECTORY ]; then mkdir -p $BACKUP_FILES_DIRECTORY; fi
    if [ -d $RESTORE_FILES_DIRECTORY ]; then mkdir -p $RESTORE_FILES_DIRECTORY; fi
    if [ -d ~/assignment1/tmp/ ]; then mkdir ~/assignment1/tmp; fi
    if [ -f ~/assignment1/tmp/backuplist.txt ]; then touch ~/assignment1/tmp/backuplist.txt; fi
}



Thanks again, why is this though, works without I think, is it just for read-ability?

colucix 03-07-2013 08:58 AM

Actually using the option -p you don't really need to check for the existence of the directory: if the directory exists mkdir -p exits silently and the timestamp of the directory is left untouched. If updating the timestamp of the file is not a problem, the same is true for the touch command: it simply updates the timestamp of existing files.

shivaa 03-07-2013 08:58 AM

No, it's not just for readability, but part of syntax. Without (), it will remain incomplete and you will not be able to call it.

Code:

function()          # Defining
{                  # Starting function body
Something          # Body of function         
}                  # Closing
function            # Calling function


Batistuta_g_2000 03-07-2013 09:55 AM

Quote:

Originally Posted by colucix (Post 4906743)
Actually using the option -p you don't really need to check for the existence of the directory: if the directory exists mkdir -p exits silently and the timestamp of the directory is left untouched. If updating the timestamp of the file is not a problem, the same is true for the touch command: it simply updates the timestamp of existing files.

so doing below is essentially pointless - I should just do a:

mkdir -p $BACKUP_FILES_DIRECTORY unless I want to do something else after.

Code:

    if [ ! -d $BACKUP_FILES_DIRECTORY ]; then
          mkdir -p $BACKUP_FILES_DIRECTORY
    fi


Thanks

chrism01 03-07-2013 05:30 PM

Pretty much, unless you want to eg notify someone at that point.

David the H. 03-09-2013 08:55 AM

Code:

setup_dirs() {
    mkdir -p "$IMPORTANT_FILES_DIRECTORY" "$BACKUP_FILES_DIRECTORY" \
            "$RESTORE_FILES_DIRECTORY" "$HOME/assignment1/tmp"

    touch "$HOME/assignment1/tmp/backuplist.txt"
}

mkdir can accept multiple files at once, so there's no need to use a loop unless you want to explicitly test for existence first. Which you might still consider doing in some cases, since the external mkdir process would not spawn unless needed.

The recommended, posixly-correct syntax for function definitions is "funcname()" The function keyword is a bash extension, so "function funcname" is also correct, as long as you're using that shell. In any case you should choose one or the other, and not combine them into "function funcname()". While bash will still accept that without complaint, it isn't really correct.

"~" is really there mostly for interactive use. In scripts you should generally use the "$HOME" variable instead.

Finally, don't forget to always quote all of your variable expansions, to avoid possible word-splitting and globbing expansion problems. It's also generally recommended to avoid using all uppercase variable names, to differentiate them from the uppercase built-in environmental variables.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Scripting With Style

Batistuta_g_2000 03-11-2013 09:29 AM

Quote:

Originally Posted by David the H. (Post 4907973)
Code:

setup_dirs() {
    mkdir -p "$IMPORTANT_FILES_DIRECTORY" "$BACKUP_FILES_DIRECTORY" \
            "$RESTORE_FILES_DIRECTORY" "$HOME/assignment1/tmp"

    touch "$HOME/assignment1/tmp/backuplist.txt"
}

mkdir can accept multiple files at once, so there's no need to use a loop unless you want to explicitly test for existence first. Which you might still consider doing in some cases, since the external mkdir process would not spawn unless needed.

The recommended, posixly-correct syntax for function definitions is "funcname()" The function keyword is a bash extension, so "function funcname" is also correct, as long as you're using that shell. In any case you should choose one or the other, and not combine them into "function funcname()". While bash will still accept that without complaint, it isn't really correct.

"~" is really there mostly for interactive use. In scripts you should generally use the "$HOME" variable instead.

Finally, don't forget to always quote all of your variable expansions, to avoid possible word-splitting and globbing expansion problems. It's also generally recommended to avoid using all uppercase variable names, to differentiate them from the uppercase built-in environmental variables.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Scripting With Style

Thanks a million - Really great advice.


All times are GMT -5. The time now is 02:59 PM.