Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Can you tell me why I need to put /bin before the command?
Because Cron has a limited PATH
Quote:
And this #! /bin/bash is like coment right? There is # before. So, it is not need?
It is called a shebang. It consists of a number sign and an exclamation point character (#!), followed by the full path to the interpreter such as /bin/bash.
All scripts under UNIX and Linux execute using the interpreter specified on a first line.
It is needed in every script.
Can you tell me why I need to put /bin before the command?
Well, the advice is to put the full path of every command in crontab jobs. This is because the environment of crontab is different from that one you get at login. Try to put a simple
Code:
/bin/echo $PATH
in your cron job and you will discover that PATH is very limited, usually to /bin:/usr/bin. Hence a full /bin path is not really needed, but it's a good habit to avoid headache in the future!
Quote:
And this #! /bin/bash is like coment right? There is # before. So, it is not need?
Nope. Even if it starts with a hash, it's not a comment. You have to put it in the very first line of your scripts and it tell which interpreter must be used to run the subsequent statements. It is called sha-bang and has the advantage that make your script more portable, forcing to interpret commands in a particular shell environment. Also you can add options as if you run the script as (just an example):
Cron has a limited PATH, so best to give the whole path to mkdir
Code:
mkdir "/media/Multimedia/NVIDIA/$(DATE)"
will only work if the variable $DATE is set in the script
Code:
/bin/mkdir /media/Multimedia/NVIDIA/"`date`"
will work without any variables
My mistake -- $(DATE) will try to run an executable called DATE. See Command Substitution in the GNU Bash Reference Manual. I should have said that /media/Multimedia/NVIDIA/"`date`" is equivalent to "/media/Multimedia/NVIDIA/$(date)". It doesn't matter if /media/Multimedia/NVIDIA is inside or outside the quotes because it has no whitespace characters within it.
One thing I don't understand -- if it was necessary to use /bin/mkdir, why was it not also necessary to use /bin/date?
One thing I don't understand -- if it was necessary to use /bin/mkdir, why was it not also necessary to use /bin/date?
IMHO the problem was
1 test1 wasn't a script, just a txt file containing mkdir "/media/Multimedia/NVIDIA/$(DATE)"
2 Since it wasn't a script, it wasn't executed
3 mkdir "/media/Multimedia/NVIDIA/$(DATE)" doesn't work, since the variable isn't set, hence it wasn't even a script, and the syntax isn't correct
4 mkdir /media/Multimedia/NVIDIA/"`date`" works, without variables, even from the command line
5 /bin is in the path from cron, but I always use full path in cron.
3 mkdir "/media/Multimedia/NVIDIA/$(DATE)" doesn't work, since the variable isn't set, hence it wasn't even a script, and the syntax isn't correct
$(<command(s)>) is the preferred successor to `<command(s)>` except for quick-and-dirty use at the command line. The reference information is on the link above. The reasons why it is preferred are in the Advanced Bash Scripting Guide, search for "The $(...) form has superseded backticks for command substitution"
Quote:
Originally Posted by repo
5 /bin is in the path from cron, but I always use full path in cron.
Ah, that's good to know. So there was no need to use /bin/mkdir but I agree with you that it is good practice, both in scripts to be run from cron and to ensure the intended executable is run, regardless of $PATH. One downside of this approach is that it may cause breakage when running on a variety of distros when executables may not be in the same directory; for this reason I'm wondering if it may be better to set $PATH in the script than to use executable full paths.
Well, so If I want to create a script. How can I do it? What I want to do is autocopy a folder from one computer to other. So is that ok?
Code:
#! /bin/bash
namefolder=$(date); mkdir "/media/Multimedia/newexpbackup/$namefolder"; tar -zcvf "/media/Multimedia/newexpbackup/$namefolder/backup.tar.gz" /home/miros/Myboxfiles
When I put this command in the bash, it works. How can I do it as script? What is the diferent between scripts and bash commands?
I tested the script above and it works.
You can compare a script with the batch file in windows.
It can contain different commands, which will be executed one by one.
It is easier to write a script, then to type the command each time at the command prompt.
You can add comments in the script, so after one year you still know where the script is for, and what it does.
If you use a script, no use to put all the commands in one line
Code:
#! /bin/bash
# script to backup my files
# Set some variables
namefolder=$(date)
#Create the directory
mkdir "/media/Multimedia/newexpbackup/$namefolder"
# backup the files
tar -zcvf "/media/Multimedia/newexpbackup/$namefolder/backup.tar.gz" /home/miros/Myboxfiles
# another job well done
A scripts are a very powerfull tool to automate your daily tasks
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.