LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 03-07-2013, 06:09 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-07-2013, 06:52 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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.

Last edited by shivaa; 03-07-2013 at 06:54 AM. Reason: Typo
 
Old 03-07-2013, 08:03 AM   #3
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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
}
 
Old 03-07-2013, 08:09 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
}
 
Old 03-07-2013, 08:11 AM   #5
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Batistuta_g_2000 View Post
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
 
Old 03-07-2013, 08:15 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
}
 
Old 03-07-2013, 08:32 AM   #7
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
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?
 
Old 03-07-2013, 08:58 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 03-07-2013, 08:58 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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
 
Old 03-07-2013, 09:55 AM   #10
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
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
 
Old 03-07-2013, 05:30 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Pretty much, unless you want to eg notify someone at that point.
 
Old 03-09-2013, 08:55 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
Old 03-11-2013, 09:29 AM   #13
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
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.
 
  


Reply



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
[SOLVED] Bash Script problem running If test on user entered variables Regnets1 Linux - Newbie 3 02-17-2012 02:21 PM
Shell Scripts Variables Test Hi_This_is_Dev Linux - General 5 08-28-2010 08:21 PM
[SOLVED] Silencing the line "echo test > test/test.txt" in a shell script Arenlor Linux - General 2 06-18-2010 01:37 PM
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Korn Shell test if multiple variables are set jonlake Programming 4 04-09-2008 10:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:41 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration