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 the current cursor position.
i= To move one character Right from the current cursor position.
j= To move one line Down from the current line.
k=To move one line Up from the 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 the word.
0= To the beginning of the line in the same sentence.
$= To the end of the line in the same sentence.

(b) Character Deletion Keys/Commands :

  • Back Space(CTRL+H) : Deletes the character to the left of the cursor.
  • CTRL+D : Deletes the character under the cursor (not to the left).
  • Ctrl + W: Deletes the word to the left of the cursor.
  • Ctrl + U: Deletes the entire line to the left of the cursor.
  • Ctrl + K: Deletes the entire line to the right of the cursor.
  • Ctrl + Y: Yank (paste) the last deleted text. Works after using commands like Ctrl + U or Ctrl + K.

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 differently using an 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 users and store them into two shell variables and display its sum total.
(Program is writeen 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  --------------------

clear
for x in 1 2 3 4 5 6 7 8 9 10
do
    echo -n "$x "
done

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

clear
for num in {11..20}
do
  echo -n "$num "
done

Output: 11 12 13 14 15 16 17 18 19 20

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

clear
for num in $(seq 10 22)
do
  echo -n "$num "
done

Output:10 11 12 13 14 15 16 17 18 19 20 21 22

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

clear
for num in $(seq 10 4 30)
do
  echo -n "$num "
done

Output:
10 14 18 22 26 30 

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

clear
for num in $(seq 30 -3 10)
do
  echo -n "$num "
done

Output: 30 27 24 21 18 15 12 

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

clear
for num in $(seq 50 -1 40)
do
  echo -n "$num "
done

Output: 50 49 48 47 46 45 44 43 42 41 40 

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

clear
echo "Enter two values :"
read m
read n
echo "The output are :"
for num in $(seq "$m" "$n")
do
  echo -n "$num "
done

Output:
Enter two values :
10
20
The output are :

10 11 12 13 14 15 16 17 18 19 20

--------------------  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 that prints all the values from 1 to 10 using a 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

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

clear
 echo Enter your letter choice as add/sub/mul/div
 read x

case $x in
    
    "add") echo "Addition" ;;
    "Add") echo "Addition" ;;
   
    "sub") echo "Subtraction" ;;
    "Sub") echo "Subtraction" ;;
    
    "mul") echo "Multiplication" ;;
    "Mul") echo "Multiplication" ;;
    
    "div") echo "Division" ;;
    "Div") echo "Division" ;;
    
    *) echo "Invalid choice" ;;
esac

Output:
Enter your letter choice as add/sub/mul/div
ADD
Invalid choice

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

clear
 echo Enter your letter choice as add/sub/mul/div
 read x

case $x in
    
    "add"|"Add") echo "Addition" ;;
   
    "sub"|"Sub") echo "Subtraction" ;;
    
    "mul"|"Mul") echo "Multiplication" ;;
    
    "div"|"Div") echo "Division" ;;
    
    *) echo "Invalid choice" ;;
esac

Output:
Enter your letter choice as add/sub/mul/div
Sub
Subtraction

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

clear
 echo Enter your numeric choice from 1-12
 read x

case $x in
    
    1|3|5|7|8|10|12)echo "Month is of 31 days" ;;
    
    2) echo "Month is of 28/29 days" ;;
    
    4|6|9|11)echo "Month is of 30 days" ;;
    
    *) echo "Invalid choice" ;;
esac

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

echo "Choose any one option from the menu below :"

select choice in "Display Current Date" "List Existing Files" "Check Uptime" "Exit"
do
  case $choice in
    "Display Current Date")
      echo "The current date and time is:"
      date
      ;;
    "List Existing Files")
      echo "The files in the current directory are:"
      ls
      ;;
    "Check Uptime")
      echo "System uptime is:"
      uptime
      ;;
    "Exit")
      echo "Exiting the script. Goodbye!"
      break
      ;;
    *)
      echo "Invalid option. Please select a valid choice."
      ;;
  esac
done

Output:
Choose any one option from the menu below :
1) Display Current Date	 3) Check Uptime
2) List Existing Files	 4) Exit
#? 1
The current date and time is:
Thu Dec 12 16:25:20 UTC 2024
#? 4
Exiting the script. Goodbye!
Example : Write a shell script or Linux program to print all the array values.
# Declare an array with name X
X=(10 20 30 40 50)

# Add new values to the array X
X[5]=60

# Access and display a specific value at location 3
echo "Element at index 2: ${X[2]}"  # Output: 30

# Access and display all elements of array X
echo "All elements in Array X: ${X[@]}"  # Output: 10 20 30 40 50 60

# Get the number of elements/Length of Array X
echo "Array length: ${#X[@]}"  # Output: 6

Output:
Element at index 2: 30
All elements in Array X: 10 20 30 40 50 60
Array length: 6

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

X=(10 20 30 40 50)

# Loop through array elements
for val in "${X[@]}"
do
  echo -n "$val "
done

Output: 10 20 30 40 50

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

# Declare an array with name X
X=(10 20 30 40 50)

# Remove specific value from the array X
unset X[2]

# Access and display all elements of array X
echo "All elements in Array X after deletion: ${X[@]}"

# Remove all values from the array X
unset X

# Get the number of elements/Length of Array X
echo "Array length after deletion: ${#X[@]}"

Output:
All elements in Array X after deletion: 10 20 40 50
Array length after deletion: 0 

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

# Initialize an empty array
X=()

# Prompt(-p) the user for the number of values(n) to store
read -p "Give the value for array size? " n

# Read/store values into the array dynamically
echo "Enter $n values:"
for ((i=0; i<n; i++))
do
  read -p "Value $((i+1)): " value
  X[i]=$value # Append value to the array
done

# Display the values from the array
echo "The entered values are:"

#for val in "${X[@]}"
#do
  #echo "$val"
#done

echo "All the elements in Array X: ${X[@]}"

Output:
Give the value for array size? 5
Enter 5 values:
Value 1: 10
Value 2: 20
Value 3: 30
Value 4: 40
Value 5: 50
The entered values are:
All the elements in Array X: 10 20 30 40 50

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

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.