Learning-Linux

Learning linux commands and shell scripting!

This project is maintained by Curovearth

Part 3: Introduction to Shell Scripting

Shell Scripting Basics


In the current folder you’ll be able to see greet.sh which can be run in your terminal using the command $ bash greet.sh or $ ./greet.sh

Output:

Similarly, you can also play around with the file greetnew.sh


Generally, it is not a good idea to grant permission to a script for all user, groups and other alike. It is more appropriate to limit the execute permission to only the owner


Filters, Pipes and Variables

Pipes “ ” use the following format:
[command 1] | [command 2] | [command 3] ... | [command n]  

Some commands, such as tr, only accept “standard input” as input (not strings or filenames):

Useful features of Bash Shell


Command Substitution

A feature of the shell, which helps save the output generated by a command in a variable.

It can also be used to nest multiple commands, so that the innermost command’s output can be used by outer commands.


For the file present with the name wish.sh execute the following commands


Let us create a bash script named dirinfo.sh that takes the directory name as an argument and prints the total number of the the directories and the number of files in it.

We will make use of the find command with -type option which will list only files or directories depending upon the usage of d switch or f switch respectively.

-d - operator to check if the given directory exists or not

The command wc -l will count the lines.


Scheduling Jobs using Cron


Scheduling a shell script. Run the file diskusage.sh that prints the current time and the current disk usage statistics.

The -r option causes the current crontab to be removed.

Caution: This removes all your cron jobs. Be extra cautious when you use this command on a production server. $ crontab -r


Have Fun