Learning linux commands and shell scripting!
This project is maintained by Curovearth
#!/bin/sh
#!/bin/bash
#!/usr/bin/env python3
$ touch hello_world.sh
$ echo '#! /bin/bash >> hello_world.sh'
>>
- bash output redirection operator - appending output to file$ echo 'echo hello world' >> hello_world.sh
- printing the statement and redirecting it to the filels -l hello_world.sh
chmod +x hello_world.sh
$ ./hello_world.sh
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
greet.sh
, run the following command
$ chmod u+x greet.sh
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):
tr
(translate) - replaces characters in input text
tr [OPTIONS] [target characters] [replacement characters]
In cases like this, we can use piping to apply the command to strings and file contents.$ echo "Linux and shell scripting are awesome\!" | tr "aeiou" "_"
L_n_x _nd sh_ll scr_pt_ng _r_ _w_s_m_!
tr -c
$ var_name=value
(There are no spaces around ‘=’), below is the example
$ GREETINGS="Hello"
$ echo $GREETINGS
Hello
$ unset var_name
export var_name
$ export GREETINGS
$ env | grep "GREE"
$ GREETINGS=Hello
- Greetings is now an environment variable#
- precedes a comment;
- command separator*
- filename expansion wildcard
$ ls /bin/ba*
outputs /bin/bash
?
- single character wildcard in filename expansion
$ ls /bin/?ash
outputs /bin/bash /bin/dash
\
- escape special character interpretation
$ echo "\$1 each"
- escapes the default interpretation of $ and outputs $1 each
" "
- interpret literally, but evaluate metacharacters
echo "$1 each"
outputs each
' '
- interpret literally
echo '$1 each'
outputs $1 each
>
- redirect output to file also creates if it doesn’t exist>>
- Append output to file2>
- Redirect standard error to file2>>
- Append standard error to file<
- Redirect file contents to standard outputA 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.
$ myip=$(hostname -i)
echo $myip
outputs your ip$ ls | wc -l
$ df -h | grep overlay
df -h
- disk usage for all individual filesystems including the total usage across the server under the head overlaygrep
for overlay from the output of df -h
ls -lS /bin | head -6
For the file present with the name wish.sh execute the following commands
$ chmod +x wish.sh
$ ./wish.sh Ramesh Sannareddy
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.
$ crontab -e
min hour date_of_month month day_of_week command
30 15 * * 0 date >> sunday.txt
- append the current date to file sunday.txt at 15:30 every sundayScheduling a shell script. Run the file diskusage.sh that prints the current time and the current disk usage statistics.
$ crontab -e
$ 0 0 * * * /home/project/disksusage.sh >>/home/project/diskusage.log
$ crontab -l
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 …