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 { |
Test will work if it's passed to asisgnment. Like you can try:
function setup_dirs { Code:
if [ -d $IMPORTANT_FILES_DIRECTORY ]; then Code:
if ([ -d $IMPORTANT_FILES_DIRECTORY ]; [ -d $BACKUP_FILES_DIRECTORY ]; [ -d $RESTORE_FILES_DIRECTORY ]; [ -d ~/assignment1/tmp/ ]; [ -f ~/assignment1/tmp/backuplist.txt ]); then |
Quote:
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 { |
Use a semicolon before fi. Or try:
Code:
function setup_dirs { Code:
function setup_dirs { |
Quote:
Think I got it - Thanks Code:
#!/bin/bash |
Also, one important thing to notice that you've defined function incorrectly. Use () after function name while defining it, as:
Code:
function setup_dirs() { Code:
function setup_dirs() { |
Quote:
Thanks again, why is this though, works without I think, is it just for read-ability? |
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.
|
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 |
Quote:
mkdir -p $BACKUP_FILES_DIRECTORY unless I want to do something else after. Code:
if [ ! -d $BACKUP_FILES_DIRECTORY ]; then Thanks |
Pretty much, unless you want to eg notify someone at that point.
|
Code:
setup_dirs() { 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 |
Quote:
|
All times are GMT -5. The time now is 02:59 PM. |