Table of Contents hide
3. Popular Online Link to Execute/Run Shell Program without Installation of Linux in the System

Steps to Create a Linux Program

Step 1 – Open Linux in command mode and make a new directory at Linux prompt (say $) to store newly created Linux program file either online or offline and change the directory only one time.

$ mkdir prg (Press Enter)       [‘prg’ is the name of the directory we want to create]
$ cd prg

Step 2 – Now create a new fresh Linux file (say for eg- Fabonacci.sh, here .sh is the extension of Linux shell file) using Unix/Linux ‘vi’ Editor to write and save Linux program in it .

$ vi  Fabonacci.sh (Press Enter)

Step 3 – Now Press I/i button to enter into insert mode and start typing the program codes inside Fabonacci.sh file in the appeared vi Linux editor.

Step 4 – Complete the Linux program in Fabonacci.sh file .

Step 5 – Now press ‘Esc‘ button to come out from Insert mode after finishing the code of the program.

Step 6 – Now again press colon (:) symbol to come out from program area and press enter. Now : symbol appears at the bottom of the editor page.

Step 7 – Now type ‘wq‘ word to come out from ‘vi’ Editor with saving the program.

Step 8 – Now, to run/execute/debug the saved program –

$ sh  Fabonacci.sh (Press Enter)          [Either Output or Error will appear – Press Enter/ ]

      • When output will appear –  Press Enter to do next job/work after seeing the result.
      • When Error will appear – Press  CTRL+D – Again, repeat step 2-7 to edit the program and save it and execute it again. The same process is repeated many times until correct output will come.

$ vi  Fabonacci.sh (Press Enter) . . . and follow step2-8

Keys used during Editing a Linux or Shell Program for movement cursor in VI Editor

(a) Cursor Movement Keys/Commands :

h= To move one character Left from current cursor position.
i= To move one character Right from current cursor position.
j= To move one line Down from current line.
k=To move one line Up from current line.
spacebar= To move rightward
backspace= To move leftward
w= To move forward/next word from current word.
b=To move backward/back word from current word.
e= To move at the end of word.
0= To the beginning of line in same sentence.
$= To the end of line in same sentence.

(b) Character Deletion Keys/Commands :

Popular Online Link to Execute/Run Shell Program without Installation of Linux in the System

Link1  Link2  Link3  Link4  Link5  Link6  

Example : Write a shell script or Linux program to show messages in different way using echo statement.
At $ or Linux Command Prompt :
   
   $ echo Shell script is case sensitive (press enter)

   Output : Shell script is case sensitive

   --------------------------------------  OR --------------------------------------
   $ echo Welcome       U     All       in Codershelpline (press enter)

   Output : Welcome U All in Codershelpline   
   --------------------------------------  OR --------------------------------------
   $ echo "Welcome U All in Codershelpline" (press enter)

   Output : Welcome U All in Codershelpline   
   --------------------------------------  OR --------------------------------------
   $ echo "Welcome   U     All in        Codershelpline" (press enter)

   Output : Welcome   U     All in        Codershelpline
  --------------------------------------  OR --------------------------------------
   $ echo "Welcome   U     All in        Codershelpline"; (press enter)

   Output : Welcome   U     All in        Codershelpline
 --------------------------------------  OR --------------------------------------
   $ echo 'Welcome   U     All in        Codershelpline' (press enter)

   Output : Welcome   U     All in        Codershelpline
--------------------------------------  OR --------------------------------------
echo Welcome  
echo 2022 

Output : 
Welcome  
2022
--------------------------------------  OR --------------------------------------
echo -n Welcome 
echo 2022 

Output : 
Welcome2022 
--------------------------------------  OR --------------------------------------
echo -n Welcome 
echo -n "  " 
echo 2022 

Output : 
Welcome  2022 

NB : In linux by default each echo command print contents with a new line but to print the contents in same horozontal line we use -n.

--------------------------------------  OR --------------------------------------

At vi Editor as a Shell script File :

   (do the code Similar as $ prompt but without $ in a shell file)

   echo Welcome U in Codershelpline (press enter)

   Output : Welcome U in Codershelpline  [After execution of created shell script file] 
Example : Write a shell script program in Linux to assign static or user values in shell variables and display it.
  # value initialization in shell variable

  clear
  x=24
  y=57
  z=96

  echo $x $y $z
  24 57 96
--------------------------------------  OR --------------------------------------
  clear  
  x=24; y=57; z=96.352;

  echo $x $y $z
  24 57 96.352  
--------------------------------------  OR --------------------------------------
  clear
  x="IGNOU"; y=57; z="Robert3258";

  echo $x $y $z
  IGNOU 57 Robert3258
--------------------------------------  OR --------------------------------------
  clear
  echo Enter first value
  read x
  25

  echo $x
  25

  echo Enter second value
  read y
  55

  echo $y
  55
--------------------------------------  OR --------------------------------------
  clear
  echo Enter three values
  read x y z
  25 58 69

  echo $x $y $z
  25 58 69
--------------------------------------  OR --------------------------------------
  echo Enter values for variables
  read x1 x2 x3
  55 12 38 97 95 85

  echo $x1 $x2 $x3
  55 12 38 97 95 85

  echo $x1 
  55

  echo $x2 
  12

  echo $x3
  38 97 95 85
Example : Write a shell script or program in Linux to calculate constant or shell variables values and display it.
$ expr 1 + 2 (press enter)
3

$ expr 7 - 2 (press enter)
5

$ expr 7 '*' 2 (press enter)
14

$ expr 7 \* 2 (press enter)
14

$ expr 9 / 2 (press enter)
4

$ expr 7 % 2 (press enter)
1
Example : Write a shell script/Linux program to take two values from the uers and store it into two shell variables and display its sum total.
At vi Editor in a Shell File :

  clear
  echo Enter first value
  read x

  echo Enter second value
  read y

  z=`expr $x + $y`  
  echo The addition result of $x + $y is = $z


NB : ` is Smart quotes,not single quotes, located above tab button is used with expr statements.
Example : Write a shell script or Linux program to take required input values from the users at run time to calculate and display simple interest value.
 clear
 echo Enter Principal value :
 read p

 echo Enter rate value :
 read r

 echo Enter time value :
 read t

 si=`expr '(' $p \* $r \* $t ')' / 100`
 echo Simple Interest is '=' $si
Example : Write a shell script or Linux program to display the accepted value is Larger, Smaller or Equal using If statement.
echo Enter first value
read a

echo Enter second value
read b

if [ $a == $b ]
then
    echo "Both value is same"
fi

if [ $a -gt $b ]
then
    echo "First value is Larger"
fi

if [ $a -lt $b ]
then
    echo "Second value is Larger"
fi
Example : Write a shell script or Linux program to check whether the accepted value is Odd or Even using If-else Statement.
clear
echo -n "Enter a number:"
read x

if [ `expr $x % 2` == 0 ]
then
	echo "$x is Even no."
else
	echo "$x is Odd no."
fi
Example : Write a shell script or Linux program to display the name of days of a week when we enter numeric value from 1-7 using If-elif—else Statement.
clear
 echo Enter your choice from 1-7
 read x

if [ $x == 1 ]
then
   echo "Sunday"
elif [ $x == 2 ]
then
   echo "Monday"
elif [ $x == 3 ]
then
   echo "Tuesday"
elif [ $x == 4 ]
then
   echo "Wednesday"
elif [ $x == 5 ]
then
   echo "Thursday"
elif [ $x == 6 ]
then
   echo "Friday"
elif [ $x == 7 ]
then
   echo "Saturday"
else
   echo "Invalid Choice"
fi
Example : Write a shell script or Linux program to print all the values from 1-10 present in the list using For loop.
for x in 1 2 3 4 5 6 7 8 9 10
do
    echo $x
done

Output:
1
2
3
4
5
6
7
8
9
10

--------------------  OR  --------------------

for x in 1 2 3 4 5 6 7 8 9 10
do    
    if [ $x == 8 ]
    then
        break
    fi    
    echo $x
done

Output:
1
2
3
4
5
6
7

--------------------  OR  --------------------

for x in 1 2 3 4 5 6 7 8 9 10
do    
    if [ $x == 5 ]
    then
        continue
    fi    
    echo $x
done

Output:
1
2
3
4
6
7
8
9
10

--------------------  OR  --------------------

fruits="Apple Orange Mango Banana"

for x in $fruits
do
    echo $x
done

Output:
Apple
Orange
Mango
Banana

NB:
- The for loop operates/works on lists of items mainly. 
- The loop continues for every item present in the list. 
Example : Write a shell script or Linux program to print all the values from 1-10 using While loop.
# Shell script to print no. from 1-10
clear
x=1
 while [ $x -lt 11 ]
do    
    echo $x  
    
    x=`expr $x + 1`
done

Output:
1
2
3
4
5
6
7
8
9
10
Example : Write a shell script or Linux program to print all the values from 1-10 using Until loop.
clear
x=1
 
#This loop continues until the value of x becomes greater than 10
until [ $x -gt 10 ]
do    
    echo $x    
    x=`expr $x + 1`
done

NB:
- The until loop is executed as many as times, the condition/command evaluates to false.
- This loop terminates when the condition/command becomes true.
Example : Write a shell script or Linux program to print the name of all days using the keys from 1-7 using Case-Esac statement.
 clear
 echo Enter your numeric choice from 1-7
 read x

case $x in
    
    1) echo "Sunday" ;;
   
    2) echo "Monday" ;;
    
    3) echo "Tuesday" ;;
    
    4) echo "Wednesday" ;;
    
    5) echo "Thursday" ;;
    
    6) echo "Saturday" ;;
    
    *) echo "Invalid choice" ;;
esac

--------------------  OR  --------------------

clear
 echo Enter your letter choice as a/s/m/d
 read x

case $x in
    
    "a") echo "Addition" ;;
    "A") echo "Addition" ;;
   
    "s") echo "Subtraction" ;;
    
    "m") echo "Multiplication" ;;
    
    "d") echo "Division" ;;
    
    *) echo "Invalid choice" ;;
esac

Loading

Categories: Unix/Linux OS

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.