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.
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 :
(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 them.
# 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 variable values and display them.
$ 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 or Linux program to display the Current Date and Time.
echo "Current System/Server Date and Time is = $(date)"
Output:
Current System/Server Date and Time is = Fri Jan 3 16:57:29 UTC 2025
Example: Write a shell script or Linux program to take two user values, store them in two shell variables, and display their 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 values.
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 as Larger, Smaller, or Equal using the 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 an 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
------------- OR -------------
read -p "Enter a number: " num
if ((num % 2 == 0)); then
echo "$num is even."
else
echo "$num is odd."
fi
Output:
Enter a number: 10
10 is even.
------------- OR -------------
read -p "Enter a number: " num
if ((num % 2 == 0))
then
echo "$num is even."
else
echo "$num is odd."
fi
Output:
Enter a number: 15
15 is odd.
------------- OR -------------
# SHELL SCRIPT TO CHECK PALINDROME STRING
read -p "Enter a string value : " str
rev1=$(echo $str | rev)
if [[ $str == $rev1 ]]
then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi
Output:
Enter a string value : madam
madam is a palindrome.
Example : Write a shell script or Linux program to display the name of days of a week when we enter numeric values from 1-7 using the 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 the Largest value from three values.
read -p "Enter three numbers : " a b c
if ((a > b && a > c))
then
echo "$a is the largest value."
elif ((b > a && b > c))
then
echo "$b is the largest value."
else
echo "$c is the largest value."
fi
Output:
Enter three numbers : 20 11 31
31 is the largest value.
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 --------------------
# DISPLAY THE NUMBER FROM 1 TO 10 HORIZONTALLY
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 --------------------
# DISPLAY THE NUMBER FROM 11 TO 20 HORIZONTALLY IN OTHER WAY
clear
for num in {11..20}
do
echo -n "$num "
done
Output: 11 12 13 14 15 16 17 18 19 20
-------------------- OR --------------------
# DISPLAY THE NUMBER FROM 10 TO 22 HORIZONTALLY IN OTHER WAY
clear
for ((i=10;i<=22;i++))
do
echo -n "$i "
done
Output:10 11 12 13 14 15 16 17 18 19 20 21 22
-------------------- OR --------------------
# DISPLAY THE NUMBER FROM 10 TO 22 HORIZONTALLY IN OTHER WAY
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 --------------------
# DISPLAY THE NUMBER FROM 10 TO 30 HORIZONTALLY WITH THE INTERVAL OF +4
clear
for num in $(seq 10 4 30)
do
echo -n "$num "
done
Output:
10 14 18 22 26 30
-------------------- OR --------------------
# DISPLAY THE NUMBER FROM 30 TO 10 HORIZONTALLY WITH THE INTERVAL OF -3
clear
for num in $(seq 30 -3 10)
do
echo -n "$num "
done
Output: 30 27 24 21 18 15 12
-------------------- OR --------------------
# DISPLAY THE NUMBER FROM 50 TO 40 HORIZONTALLY WITH THE INTERVAL OF -1
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 --------------------
# LINUX SHELL SCRIPT FOR FACTORIAL RESULT
read -p "Enter a number for factorial : " num
fact=1
for ((i=1; i<=num; i++))
do
fact=$((fact * i))
done
echo "Factorial of $num is $fact."
Output:
Enter a number for factorial : 5
Factorial of 5 is 120.
-------------------- OR --------------------
# LINUX SHELL SCRIPT FOR FIBONACCI SERIES
read -p "Enter number of terms for Fibonacci series : " val
a=0
b=1
echo "Fibonacci series are :"
echo -n "$a "
echo -n "$b "
for ((i=0; i<val-2; i++))
do
fn=$((a + b))
echo -n "$fn "
a=$b
b=$fn
done
echo # for cursor in next line after output
Output:
Enter number of terms for Fibonacci series : 7
Fibonacci series are :
0 1 1 2 3 5 8
-------------------- OR --------------------
# DISPLAY THE NUMBER GIVEN FROM USERS AT RUN TIME HORIZONTALLY
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 --------------------
# DISPLAY THE NUMBER VERTICALLY USING BREAK STATEMENT
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 --------------------
# DISPLAY THE NUMBER VERTICALLY USING CONTINUE STATEMENT
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
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 the 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 create and print the contents differently 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 --------------------
# LINUX SHELL SCRIPT OF SIMPLE CALCULATOR
echo "Enter two numbers:"
read num1 num2
echo "Choose operation: +, -, *, /"
read opr
case $opr in
+) echo "Addition Result is : $((num1 + num2))";;
-) echo "Subtractio Result is : $((num1 - num2))";;
\*) echo "Multiplication Result is : $((num1 * num2))";;
/) echo "Division Result is : $((num1 / num2))";;
*) echo "Invalid Choice, Change it";;
esac
Output:
Enter two numbers:
10 5
Choose operation: +, -, *, /
/
Division Result is : 2
-------------------- 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 create and print the Array values differently.
# SHELL SCRIPT TO DECLARE, STORE AND DISPLAY ELEMENT FROM OF AN ARRAY
# Declare an array with name X and display all elements
X=(10 20 30 40 50)
echo "All elements in Array X: ${X[@]}"
Output : All elements in Array X: 10 20 30 40 50
------------ OR -------------
# SHELL SCRIPT TO ADD A NEW ELEMENT IN AN ARRAY
# Declare an array with name X and display all elements
X=(10 20 30 40 50)
echo "All elements in Array X: ${X[@]}"
# Add new values to the array X
X[5]=60
echo "Array elements after adding value : ${X[@]}"
Output :All elements in Array X: 10 20 30 40 50
Array elements after adding value : 10 20 30 40 50 60
------------ OR -------------
# SHELL SCRIPT TO DISPLAY SPECIFIC ELEMENT FROM OF AN ARRAY
# Declare an array with name X
X=(10 20 30 40 50)
# Access and display a specific value at location 3
echo "Element at index 2: ${X[2]}"
Output : Element at index 2: 30
------------ OR -------------
# SHELL SCRIPT TO DISPLAY ALL ARRAY ELEMENTS USING LOOP
X=(10 20 30 40 50)
# Display array elements through Loop
for val in "${X[@]}"
do
echo -n "$val "
done
Output: 10 20 30 40 50
------------ OR -------------
# SHELL SCRIPT TO DELETE/REMOVE A SPECIFIC ARRAY ELEMENT
# Declare an array with name X
X=(10 20 30 40 50)
echo "Array elements before Deletion : ${X[@]}"
# Remove specific value from the array X
unset X[2]
echo "Array elements after specific Deletion : ${X[@]}"
Array elements before Deletion : 10 20 30 40 50
Array elements after specific Deletion : 10 20 40 50
------------ OR -------------
# SHELL SCRIPT TO DELETE/REMOVE THE ALL ARRAY ELEMENTS
# Declare an array with name X
X=(10 20 30 40 50)
echo "Array elements before Deletion : ${X[@]}"
# Remove all values from the array X
unset X
echo "Array elements after all Deletion : ${X[@]}"
Output :
Array elements before Deletion : 10 20 30 40 50
Array elements after Deletion :
------------ OR -------------
# SHELL SCRIPT TO FIND OUT THE LENGTH/SIZE OF AN ARRAY
# Declare an array with name X
X=(10 20 30 40 50)
echo "Array elements are : ${X[@]}"
# Get the number of elements/Length of Array X
echo "Array length = ${#X[@]}"
Array elements are : 10 20 30 40 50
Array length = 5
------------ OR -------------
# SHELL SCRIPT TO INPUT DYNAMIC VALUES DIFFERENTLY IN AN ARRAY
# Initialize an array of constant size 5
X=(5)
# Read/store 5 values into the array dynamically
echo "Enter 5 values:"
#for i in $(seq 0 4)
for ((i=0; i<5; i++))
do
read -p "Value $((i+1)): " value
X[i]=$value # Append value to the array
done
# Display the 5 values from the array
echo "The entered 5 values are:"
#for val in "${X[@]}"
#do
#echo "$val"
#done
echo "All the elements in Array X: ${X[@]}"
Output:
Enter 5 values:
Value 1: 10
Value 2: 20
Value 3: 30
Value 4: 40
Value 5: 50
The entered 5 values are:
All the elements in Array X: 10 20 30 40 50
------------ OR -------------
# SHELL SCRIPT TO DISPLAY THE DYNAMIC VALUES DIFFERENTLY IN AN ARRAY
# Initialize an array of constant size 5
X=(5)
# Read/store 5 values into the array dynamically
echo "Enter 5 values:"
#for i in $(seq 0 4)
for ((i=0; i<5; i++))
do
read -p "Value $((i+1)): " value
X[i]=$value # Append value to the array
done
# Display the 5 values from the array
echo "The entered 5 values are:"
for i in $(seq 0 4)
do
echo -n "${X[$i]} "
done
#for ((i=0;i<=4;i++))
#do
#echo -n "${X[$i]} "
#done
Output :
Enter 5 values:
Value 1: 10
Value 2: 20
Value 3: 30
Value 4: 40
Value 5: 50
The entered 5 values are:
10 20 30 40 50
------------ OR -------------
# SHELL SCRIPT TO STORE AND DISPLAY DYNAMIC VALUES
# Initialize an array of constant size 5
X=(5)
# Read/store 5 values into the array dynamically
echo "Enter 5 values:"
#for i in $(seq 0 4)
for ((i=0; i<5; i++))
do
read -p "Value $((i+1)): " value
X[i]=$value # Append value to the array
done
# Display the 5 values from the array
echo "The entered 5 values are:"
len=${#X[@]}
for ((i=0;i<=$len-1;i++))
do
echo -n "${X[$i]} "
done
Output :
Enter 5 values:
Value 1: 1
Value 2: 2
Value 3: 3
Value 4: 4
Value 5: 5
The entered 5 values are:
1 2 3 4 5
------------ OR -------------
# SHELL SCRIPT TO REVERSE ARRAY ELEMENTS
X=(5)
# Read/store 5 values into the array dynamically
echo "Enter 5 values:"
#for i in $(seq 0 4)
for ((i=0; i<5; i++))
do
read -p "Value $((i+1)): " value
X[i]=$value # Append value to the array
done
# Display the 5 values from the array
echo "The entered 5 values in reversed are:"
for ((i=9;i>=0;i--))
do
echo -n "${X[$i]} "
done
Output:
Enter 5 values:
Value 1: 10
Value 2: 20
Value 3: 30
Value 4: 40
Value 5: 50
The entered 5 values in reversed are:
50 40 30 20 10
------------ OR -------------
# SHELL SCRIPT TO COPY ONE ARRAY ELEMENTS INTO ANOTHER
X=(5)
Y=(5)
# Read/store 5 values into the array dynamically
echo "Enter 5 values in first array :"
#for i in $(seq 0 4)
for ((i=0; i<5; i++))
do
read -p "Value $((i+1)): " value
X[i]=$value # Append value to the array
done
# Copy the Values into other array
for ((i=0;i<=9;i++))
do
Y=("${X[@]}")
#Y[$i]="${X[$i]}"
done
echo "The copied elements in other Array are : ${Y[@]}"
Output :
Enter 5 values in first array :
Value 1: 2
Value 2: 4
Value 3: 6
Value 4: 8
Value 5: 10
The copied elements in other Array are : 2 4 6 8 10
------------ OR -------------
# SHELL SCRIPT TO ACCEPT ARRAY SIZE & VALUES DYNAMICALLY
# 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
Example : Write a shell script or Linux program to print the String Values differently.
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.
------------ OR -------------
# SHELL SCRIPT TO REVERSE THE STRING
read -p "Enter a string : " str
echo "Reversed string is : $(echo $str | rev)"
Output:
Enter a string : Welcome
Reversed string is : emocleW
0 Comments