Blog

Unix Important Commands and Shell Scripting Programs

Unix Important Commands and Shell Scripting Programs

Hi Techies, You are here because of your search keyword related to Unix Important Commands and Shell Scripting Programs. We are always welcomes you to the our new tutorial Unix and Shell scripting. I have searched many websites and i saw lot of theories in all tutorials. When reading that theories, its feeling like difficult to learn any tutorials and it will reduce our interest of learning. So i am here providing what is exactly wanted or needed to visitors and fulfill their queries. At the same time we don’t miss any important concepts of Unix Commands and Shell Scripting in this tutorial.

tux

  1. Unix Important Commands
  2. Shell Scripting Programs

The above Two Topics are going to discuss majorly here…

Before that, You should know the basics of Unix…

What is an UNIX?

  • UNIX is an computer operating system like windows.
  • UNIX is a stable, multi-user, multi-tasking system for servers, desktops and laptops.
  • UNIX is not an acronym, therefore UNIX doesn’t have a full expansion.
  • UNIX systems also have a graphical user interface (GUI) like Microsoft Windows. However, the knowledge of UNIX is required for operations. For example still we are using command prompt for telnet sessions in windows.

If you are willing to learn the Unix/Linux basic commands and shell script but you do not have a setup for the same, then do not worry. We are providing screenshots of all functions with examples.

UNIX Types

There are many different versions of UNIX, although they share common similarities. In that Sun Solaris, GNU/Linux, and MacOS X are most popular types of UNIX.

Parts of UNIX

The kernel, the shell and the programs are the three parts of UNIX.

Kernel: it allocates time and memory to programs and handles the filestore and communications in response to system calls.

Shell: As an illustration of the way that the shell and the kernel work together, suppose a user types rm myfile (which has the effect of removing the file myfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on myfile. When the process rm myfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

Advantages of UNIX

  • Many peoples can able to access UNIX computer at the same time. So its called as Multi user system
  • A user can also run multiple programs at the same time. hence UNIX is called multitasking.

1. Unix Important Commands

UNIX Command Description
cal To view calendar
passwd To change your password
ls lists the contents of your current working directory
ls -a To view hidden files
whoami To view the current user of the system
users List out the current users of UNIX operating system
who To view who is logged on UNIX operating system and what they are doing?
w To view who is logged on UNIX operating system
logout To logging out the system
mkdir To create new directory
cd files Change Current Directory to files or enter into files directory
vi filename To create new file or view existing file
cat To display the content of file
cat -b To display the content of file with Line numbers
cp source_file destination_file Copying the files – Its create copy of source file in the name of destination_file
mv Renames a file

mv ram ragu – Here this command renames ram file into ragu

rm To remove the files.

rm ram – This command removes the ram file in current directory

System shutdown Commands

halt Brings the system down immediately.
init 0 Powers off the system using predefined scripts to synchronize and clean up the system prior to shutdown
init 6 Reboots the system by shutting it down completely and then bringing it completely back up
poweroff Shuts down the system by powering off.
reboot Reboots the system.
shutdown Shuts down the system.

2. Shell Scripting Programs

How to check Shells installed in unix operating system?
$cat /etc/shells [Use this command to view Shells installed in unix operating system] /bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/zsh
/bin/ksh

bash shell is here most powerfull shell.

cat /etc/passwd – you can view all the users
How to change the shell

chsh [Type this command to change the shell and give respective login when login]

whoami – It displays somebody who logged in right now.

who – It displays all the users that are currently logged on to the system

users all the users that are currently logged in a little bit of different format comparing who command

uname – It shows You are logged on to a linux machine

uname -r – It shows your kernell release

echo “Hi There” Command Dispalys Hi There
clear the screen using clear command

bash script file ends with .sh extension

A very first line of your script is the name of your shell with #!

#!/bin/bash which is going to act as middle lane between shell script and kernel

Write any comment or description followed by #

# this is my first script

Shell script is a list of unix commands written in a text file

ls -l – It shows permissions, owner, last modification date and time and file name of the file

Use chmod command to give permission to the script file

chmod u+x scriptfile.sh

it will give the excute permission to the owner of the file.

#!/bin/bash
clear
echo “————”
echo “First script”
echo “————”

Output:

————
First script
————

Variables dont have Data types and length. We dont need to declare the variables.

a=’raj,
b=’kumar’
echo $a when executing this you will get raj
echo $b when executing this you will get kumar

How to concatenate the both?

c=$a$b
echo $c when executing this you will get rajkumar

If you want spaces between the name use double quotes and give space between them like c=”$a $b”

echo $c execute this command you will get raj kumar

What is read command in unix shell scripting?

read -p ‘What is your name’ nm
If you execute above command you will get output as What is your name. If you type your name to answer for the question for example rajkumar, Then the answer will be assigned to nm.

echo $nm will give the output as rajkumar

#!/bin/bash
#This is a shell script to ask user his/her input
clear
read -p “What is your name : ” nm
echo “Hello $nm , How are you?”

The above script asks you What is your name, if you answers as raj. It will ask again Hello raj , How are you?

How to run the script without giving permission?

Use bash script.sh – It will work

How do u do mathematical calculation in shell scripting?

a=”10″
b=”20″
echo $a – you will get 10
echo $b – you will get 20

c=a+b
echo $c – It will give a+b

c=$a+$b
echo $c – It will give 10+20

c=$(( $a + $b )) —use parenthesis
echo $c – It will give 30

A simple calculator shell scripting

#!/bin/bash

#A simple calculator
clear
echo “—————————————-”
readp -p “Please enter the first number : ” a
readp -p “Please enter the second number : ” b

sum=$(( $a + $b ))
sum=$(( $a – $b ))

echo ” $a + $b = $sum”
echo ” $a – $b = $sub”
echo “—————————————-”

Save above script and execute. Probabily It will ask first and second number. Give 100 and 50.

Your output displays like this…

—————————————-
Please enter the first number : 100
Please enter the second number : 50
100 + 50 = 150
100 – 50 = 50
—————————————-

What is User defined variables?

We will declare the variables like a=2 and b=4 These User defined variables.

System Defined Variables?

System Defined Variables are the variables already defined by the system. Also valuses are already assigned to that variables that we are using for our own benefits.

How to see the System Defined Variables?
Type env, you will get like this and more…

PWD=/home/raj
HOME=/home/raj
TMP=/tmp
SHELL=/bin/bash
USER=raj

#!/bin/bash

# Some basic system defined variables
clear
echo “Value of variable HOME = $HOME”
echo “Value of variable SHELL = $SHELL”
echo “Value of variable USER = $USER”
echo “Value of variable PWD = $PWD”
echo “Value of variable PATH= $PATH”

Run this above script, you can get the system defined variables

Here we learned about Types of shells, Variables(User, System), Mathematical calculation using shell scripting and some basic shell scripts.

What is the difference between the single quote and double quotes in shell scripts?

a=’raj’ – It is Literal (Whatever you write in single quotes is literal)
b=’$a’
echo $b we can get $a not raj.

use double quotes you will get actual value.
b=”$b”
echo $b You can get raj now

Double quotes:

Whenever you want any variable to be evaluated before getting the sign to the another variable or before getting printed to the another variable then you should use double quotes.

Single Quotes:

If you want the stings to be literally as it is to be assigned to another variable or printed, you should use single quotes

Inverted Quotes:

a=`whoami`
echo $a
output= raj

k=`hostname`
echo $k
sql.example.com

p=`users`
echo $p
raj rajkumar

It displays the users

#!/bin/bash

a=`whoami’
if [ $a = ‘root’ ]; then
echo ” You are authorized to execute this script”
else
echo ” You are not authorized to execute this script”
exit
fi
echo “Hello there”
date
cal

If we execute above script in root user login we can able to view date and calender

other wise it throws You are not authorized to execute this script

#!/bin/bash

a=`whoami’
if [ $a = ‘root’ ]; then
echo ” You are authorized to execute this script”
elif [ $a = ‘raj’ ]; then
echo ” You are also authorized to execute this script”
else
echo ” You are not authorized to execute this script”
exit
fi
echo “Hello there”
date
cal

If either root or user raj were able to execute the command and they can able to see the result of date and calender command.

Number comparision in shell scripting

= -eq —equal
!= -ne —not equal
< -lt —less than
> -gt —greater than
>= -ge —greater than or equal
<= -le —less than or equal

How to check file or directory exists or not in shell scripting?

#!/bin/bash

read -p “Enter the name of the file ” file
if [ -f $file ]; then
echo “File named #file exists”
else
echo “File named $file does not exist”
fi

If you give the file name it will tell whether the file file is exists or not.

-d ===> To check the directory is exists or not
-x ===> To check Whether the file executable or not
-r ===> To check Whether the file readable or not
-w ===> To check Whether the file writable or not
-l ===> To check Whether the file link or not

#!/bin/bash

# Example of a for loop

for i in 1 2 3
do
echo “Value of variable i = $i”
done

Output :

Value of variable i = 1
Value of variable i = 2
Value of variable i = 3

What is seq command?

seq 1 2 10

Output:
1
3
5
7
9

Here 1 is starting point 2 is increment 10 is ending point

seq 1 1 5

Output:
1
2
3
4
5

Here 1 is starting point 1 is increment 5 is ending point

#!/bin/bash

# Example of a for loop

num=`seq 1 1 100`
for i in $num
do
echo “Value of variable i = $i”
done

This will replaces i by 1 to 100 like below…

Value of variable i = 1
Value of variable i = 2
.
.
.
.
.
Value of variable i = 100

===============
#!/bin/bash

# Example of a for loop

for i in 1 A 3
do
echo “Value of variable i = $i”
done

If we change the 2nd iteration as A, we will get the output below

Output :

Value of variable i = 1
Value of variable i = A
Value of variable i = 3

================

#!/bin/bash

# Example of a for loop

files=`ls /etc`
for i in $files
do
echo “Value of variable i = $i”
done

It displays entire files in /etc

output:

Value of variable i = file1
Value of variable i = file2
Value of variable i = file3

=========================

#!/bin/bash

# Example of a for loop

users=`cut -d’:” -f1 /etc/passwd`
for i in $users
do
echo “Value of variable i = $i”
done

Cut the entire line into fields
delimiter -d which separates by colon[ : ]here
-f1 ===> displays field 1

output:

Value of variable i = raj
Value of variable i = root

================

#!/bin/bash

# Example of a for loop

users=`cut -d’:” -f1 /etc/passwd`
for i in $users
do
if [ $i != ‘root’ ]; then
echo “Please wait. Changing the password for user $i”
echo “1234” | passwd –stdin $i
done

It changes the passwords for all users except root

==================

grep pmon ==>to view database currently running or not

#!/bin/bash

# how to logon to oracle database

export ORCALE_SID=focus1
sqlplus raj/raj <<EOF
show user
select sysdate from dual;
EOF

oracle data base name = focus1

Use ! or EOF

Output:

SQL> USER is ‘raj’
SQL>
SYSDATE
——–
23-DEC-13

============

Output Redirection in Shell scripting

One is >
Second is >>

whoami > a.txt
cat a.txt

output :
raj

if we use >> it will append to the existing file

whoami >> a.txt
cat a.txt

output :
raj
raj

What is standard error and how to capture standard error output?

Use the command 2> or 2>> or &>

ls -z 2> a.txt
cat a.txt
ls: invalid option — ‘z’
try ‘ls –help’ for more information.

ls -z 2>> a.txt
cat a.txt
ls: invalid option — ‘z’
try ‘ls –help’ for more information.
ls: invalid option — ‘z’
try ‘ls –help’ for more information.

clear
ls -z &> a.txt
cat a.txt
ls: invalid option — ‘z’
try ‘ls –help’ for more information.

$ amberson symbol

=================

wc – < /etc/passwd ===> std in
731

<< (here document)
< (stdin)
&>> (both stdout and stderr)

Detaily Updated Soon…

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
error: Content is protected !!