Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
}
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, 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
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
}
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?
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.