While we work in the terminal for hours, scripting can be vital. They can make our work easier and save the day for us. Scripting provides us the ability to automate various activities, hence, running scripts is very important. But how do you actually write a script and run it? Well, that’s what I’ll be teaching you guys here.
What is to be done?
- Firstly, we’re going to create a directory (folder) called “scripts” in “/” that is gonna be home to all the scripts we write.
- Then, we’re going to assign the path to that directory (scripts) in PATH variable, either permanently or temporarily.
- Now, we’re gonna write our own script in a file called “script1” inside the “scripts” directory.
- After that, we’re gonna make that file executable.
- And finally, we’ll be running that script.
/]# cd / #moves you to /
/]# mkdir scripts #makes a directory called scripts within / /]# PATH=$PATH:/scripts/ #adds the path to the scripts directory where the scripts you write will be stored in the path variable
Note that this is only a local variable, in order to make the change permanent, you should edit the ‘bashrc’ file.
Edit the /etc/bashrc file to make this variable system wide, or edit the ~/.bashrc file to make this a user specific change.
/]# vi /scripts/script1/ #makes a file called script1 in the scripts directory.
Now, you should write your script there and save and exit.
Before we can actually run the script, we need to change the permissions and make it executable.
/]# chmod a+x /scripts/script1 #makes the file executable
Here, to run the script, just type the name of the file where the script is saved!
That’s it! You’re done!
Now, you can make and run your own script, congratulations!
If you guys have any problem running the script, make sure to drop a comment below.