Shell Scripting

Linux -6

·

5 min read

The shell is a command interpreter, it takes each command and passes it to the operating system kernel to be acted upon. It then displays the results of this operation on your screen.

Different users may use different shells. Initially, your system administrator will supply a default shell, which can be overridden or changed. The most commonly available shells are:

• Bourne shell (sh)

• C shell (csh)

• Korn shell (ksh)

• TC Shell (tcsh)

• Bourne Again Shell (bash)

#!/bin/bash: It is called she-bang. It is used to instruct the operating system to use bash as a command interpreter.

There are different shebang lines for each different shell.

#!/bin/bash --> the script should always be run with bash, rather than another shell.

#!/bin/sh --> the interpreter should be a Bourne shell, not Bash.

#!/bin/csh --> interpreter here is a C shell.

#!/bin/ksh --> interpreter is Korn shell.


Write a bash script createdirectories.sh that when the script is executed with three given arguments (one is the directory name and second is the start number of directories and third is the end number of directories ) it creates a specified number of directories with a dynamic directory name.

#! /bin/bash

dir_name=$1

start=$2

enf=$3

for (( i=$2; i<=$3; i++ ))

do

mkdir $1$i

done


Create a Script to backup all your work done till now.

#!/bin/bash

src=/Users/ibrahims/script2/backup.sh tgt=/Users/ibrahims/my_backup/

backupfile=$tgt/new_backup.tar.gz

tar -czvf $backupfile $src


Automate your scripts and tasks using Cron and Crontab

Cron is a service in Linux that helps users in scheduling their tasks which could be run at specific intervals like hourly, daily, or on weekly basis.

We will use the crontab command to edit the file and automate our backup script

Use the command Crontab -e and it will open an editor:

Expression of Crontab scheduled task:

  • */1 represents a minute

  • * represents hour

  • * represents day

  • * represents month

  • * represents week

You can modify the scheduled task according to your task requirements


If else in Shell Scripting by comparing 2 numbers.

#!/bin/bash

num1=80

num2=20

if [ $num1 -gt $num2 ]

then

echo "$num1 is greater than $num2"

else

echo "$num2 is greater than $num1"

fi


Checking Disk Space

df -h | xargs | awk '{print $13"/$15"}'


SSH - Secure Shell

SSH or Secure Shell is a network communication protocol that enables two computers to communicate

SSH public key authentication use case, it is rather typical that the users create (i.e. provision) the key pair for themselves. SSH implementations include easily usable utilities for this (for more information see ssh-keygen and ssh-copy-id).

SSH key pair includes two keys

  1. A public key that is copied to the SSH server(s).

  2. Anyone with a copy of the public key can encrypt data which can then only be read by the person who holds the corresponding private key.

  3. Once an SSH server receives a public key from a user and considers the key trustworthy, the server marks the key as authorized in its authorized_keys file. Such keys are called authorized keys.

A private key that remains (only) with the user.

  1. The possession of this key is proof of the user's identity. Only a user in possession of a private key that corresponds to the public key at the server will be able to authenticate successfully.

  2. The private keys need to be stored and handled carefully, and no copies of the private key should be distributed. The private keys used for user authentication are called identity keys.

The following simple steps are required to set up public key authentication (for SSH):

  1. Key pair is created (typically by the user). This is typically done with ssh-keygen.

  2. Private key stays with the user (and only there), while the public key is sent to the server. Typically with the ssh-copy-id utility.

  3. Server stores the public key (and "marks" it as authorized).

  4. Server will now allow access to anyone who can prove they have the corresponding private key.

To generate an SSH key pair, run the command ssh-keygen.

ssh-keygen

Your public and private keys will be generated. There will be two different files. The one named id_rsa is your private key. The one named id_rsa.pub is your public key.

Method 1: Using ssh-copy-id Now that you have an SSH key pair, you're ready to configure your app's system user so you can SSH using your private key.

To copy your public key to your server, run the following command. Be sure to replace "ip_address" with your server's IP address and Username with the name of the system user your app belongs to

ssh-copy-id Username@ip_address

Method 2: Manual Configuration If you don't have the ssh-copy-id command (for example, if you are using Windows), you can instead SSH into your server and manually create the .ssh/authorized_keys file so it contains your public key. First, run the following commands to make create the file with the correct permissions.

Copy your public SSH key to remote server copied under ~/.ssh/authorized_keys)

Dont share public key to anyone

~/.ssh || mkdir ~/.ssh)

touch ~/.ssh/authorized_keys)

Next, edit the file .ssh/authorized_keys using your preferred editor. Copy and paste your id_rsa.pub file into the file.

Log In Using Your Private Key

ssh Username@ip_address

Granting Access to Multiple Keys

  1. The .ssh/authorized_keys file you created above uses a very simple format.

  2. It can contain many keys as long as you put one key on each line in the file. I

  3. f you have multiple keys (for example, one on each of your laptops) or multiple developers you need to grant access to, just follow the same instructions above using ssh-copy-id or manually editing the file to paste in additional keys, one on each line.

  4. When you're done, the .ssh/authorized_keys file will look something like this (don't copy this, use your own public keys).