Steps to Create a Linux Program
Step 1 – Open Linux in command mode and make a new directory at the 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- Fibonacci.sh, here .sh is the extension of a Linux shell file) using Unix/Linux ‘vi’ Editor to write and save the Linux program in it.
$ vi Fabonacci.sh (Press Enter)
Step 3 – Now Press I/i button to enter insert mode and start typing the program codes inside Fabonacci.sh file in the vi Linux editor.
Step 4 – Complete the Linux program in the Fibonacci.sh file.
Step 5 – Now press the ‘Esc‘ button to come out of Insert mode after finishing the code of the program.
Step 6 – Now again press the colon (:) symbol to come out of the 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 the ‘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 the next job/work after seeing the result.
- When Error will appear – Press CTRL+D – Again, repeat steps 2-7 to edit the program, save it, and execute it again. The same process is repeated many times until the correct output comes.
-
$ vi Fabonacci.sh (Press Enter) . . . and follow step2-8
Keys used during editing a Linux or Shell Program for the movement of the cursor in the 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 to 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 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 -------------
echo "Enter a number:"
read num
if [ $((num % 2)) -eq 0 ]
then
echo "Entered Number $num is Even"
else
echo "Entered Number $num is Odd"
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 CHARACTER IS VOWEL OR CONSONANT
echo "Enter a character:"
read ch
if [[ $ch == "a" || $ch == "e" || $ch == "i" || $ch == "o" || $ch == "u" ]]
then
echo "The Input character is Vowel"
else
echo "The Input character is Consonant"
fi
------------- 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.
------------ OR -------------
# SHELL SCRIPT TO CHECK THE INPUT VALUE IS DIVISIBLE BY 5 AND 11
echo "Enter a number:"
read num
if [ $((num % 5)) -eq 0 ] && [ $((num % 11)) -eq 0 ]
then
echo "The $num is Divisible by 5 and 11"
else
echo "The $num is Not Divisible by 5 and 11"
fi
------------ OR -------------
echo "Enter Username:"
read uname
echo "Enter Password:"
read pass
if [ "$uname" = "admin" ] && [ "$pass" = "1234" ]
then
echo "Login Successful"
else
echo "Invalid Username or Password"
fi
Example : Write a shell script or Linux program to display the names of the days of the 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
------------- OR -------------
# SHELL SCRIPT TO CHECK NUMBER IS NEGATIVE OR POSITIVE
echo "Enter a number:"
read num
if [ $num -gt 0 ]
then
echo "Given Number is Positive"
elif [ $num -lt 0 ]
then
echo "Given Number is Negative"
else
echo "Given Number is Zero"
fi
------------- OR -------------
# SHELL SCRIPT TO CHECK NUMBER IS LARGER
echo "Enter first number:"
read a
echo "Enter second number:"
read b
if [ $a -gt $b ]
then
echo "$a is greater than $b"
elif [ $b -gt $a ]
then
echo "$b is greater than $a"
else
echo "$a is equal to $b"
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.
------------- OR -------------
# SHELL SCRIPT TO CHECK NUMBER IS LARGER
echo "Enter three numbers:"
read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is greatest"
elif [ $b -gt $c ]
then
echo "$b is greatest"
else
echo "$c is greatest"
fi
Example : Write a shell script or Linux program to print all the values from 1 to 10 present in the list using a 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 i in {1..10}
do
echo $i
done
-------------------- OR --------------------
for i in {1..10..3}
do
echo $i
done
-------------------- OR --------------------
for i in {10..1..3}
do
echo $i
done
OUTPUT:
10
7
4
1
-------------------- OR --------------------
for ((i=10; i>=1; i--))
do
echo $i
done
-------------------- OR --------------------
for i in {A..Z}
do
echo -n "$i "
done
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
-------------------- OR --------------------
for i in {a..z}
do
echo -n "$i "
done
Output:
a b c d e f g h i j k l m n o p q r s t u v w x y z
-------------------- 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 EVEN NUMBER FROM 2 TO 20 VERTICALLY
for ((i=2; i<=20; i=i+2))
do
echo $i
done
-------------------- 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 SUM OF 1 TO 10 VALUES
sum=0
for ((i=1; i<=10; i++))
do
sum=$((sum+i))
done
echo "Sum Total is = $sum"
-------------------- OR --------------------
# LINUX SHELL SCRIPT FOR PRINTING TABLE
echo "Enter number:"
read num
for ((i=1; i<=10; i++))
do
echo "$num x $i = $((num*i))"
done
-------------------- 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 TO PRINT 7 or 10 FIBONACCI SERIES VALUES
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 --------------------
a=0
b=1
echo "Fibonacci Series Values are : "
for ((i=1; i<=10; i++))
do
echo $a
c=$((a+b))
a=$b
b=$c
done
-------------------- OR --------------------
# LINUX SHELL SCRIPT FOR REVERSE THE NUMBER
echo "Enter number:"
read num
rev=0
while [ $num -gt 0 ]
do
rem=$((num%10))
rev=$((rev*10+rem))
num=$((num/10))
done
echo "Reverse value is = $rev"
-------------------- OR --------------------
# LINUX SHELL SCRIPT TO COUNT THE DIGITS IN A NUMBER
echo "Enter number:"
read num
count=0
while [ $num -gt 0 ]
do
num=$((num/10))
count=$((count+1))
done
echo "Digits = $count"
------------------- OR --------------------
# LINUX SHELL SCRIPT TO SUM OF EACH DIGIT OF A NUMBER
echo "Enter number:"
read num
sum=0
while [ $num -gt 0 ]
do
rem=$((num%10))
sum=$((sum+rem))
num=$((num/10))
done
echo "Sum of digits = $sum"
------------------- OR --------------------
# LINUX SHELL SCRIPT FOR THE NUMBER IS PRIME OR NOT
echo "Enter number:"
read num
count=0
for ((i=1; i<=num; i++))
do
if [ $((num%i)) -eq 0 ]
then
count=$((count+1))
fi
done
if [ $count -eq 2 ]
then
echo "Prime Number"
else
echo "Not Prime"
fi
-------------------- OR --------------------
# LINUX SHELL SCRIPT TO CHECK THE NUMBER IS PALINDROME OR NOT
echo "Enter number:"
read num
temp=$num
rev=0
while [ $num -gt 0 ]
do
rem=$((num%10))
rev=$((rev*10+rem))
num=$((num/10))
done
if [ $temp -eq $rev ]
then
echo "Palindrome"
else
echo "Not Palindrome"
fi
-------------------- OR --------------------
# LINUX SHELL SCRIPT TO CHECK THE NUMBER IS ARMSTRONG OR NOT
echo "Enter number:"
read num
temp=$num
sum=0
while [ $num -gt 0 ]
do
rem=$((num%10))
sum=$((sum + rem*rem*rem))
num=$((num/10))
done
if [ $temp -eq $sum ]
then
echo "Armstrong Number"
else
echo "Not Armstrong"
fi
-------------------- 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
# DISPLAY THE PATTERN USING NESTED LOOP STATEMENT
for ((i=1; i<=5; i++))
do
for ((j=1; j<=5; j++))
do
echo -n "* "
done
echo
done
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
-------------------- OR --------------------
for ((i=1; i<=5; i++))
do
for ((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
Output:
*
* *
* * *
* * * *
* * * * *
-------------------- OR --------------------
for ((i=5; i>=1; i--))
do
for ((j=1; j<=i; j++))
do
echo -n "* "
done
echo
done
Output:
* * * * *
* * * *
* * *
* *
*
-------------------- OR --------------------
for ((i=1; i<=5; i++))
do
for ((j=1; j<=i; j++))
do
echo -n "$j "
done
echo
done
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
-------------------- OR --------------------
for ((i=1; i<=5; i++))
do
for ((j=1; j<=i; j++))
do
echo -n "$i "
done
echo
done
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
-------------------- OR --------------------
num=1
for ((i=1; i<=5; i++))
do
for ((j=1; j<=i; j++))
do
echo -n "$num "
num=$((num+1))
done
echo
done
Output:(Floyd’s Triangle Shape)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
-------------------- OR --------------------
for i in A B C D E
do
for j in A B C D E
do
echo -n "$j "
if [ "$j" = "$i" ]
then
break
fi
done
echo
done
Output:
A
A B
A B C
A B C D
A B C D E
-------------------- OR --------------------
rows=5
for ((i=1; i<=rows; i++))
do
for ((j=i; j<rows; j++))
do
echo -n " "
done
for ((k=1; k<=((2*i-1)); k++))
do
echo -n "*"
done
echo
done
Output:
*
***
*****
*******
*********
-------------------- OR --------------------
rows=5
for ((i=rows; i>=1; i--))
do
for ((j=rows; j>i; j--))
do
echo -n " "
done
for ((k=1; k<=((2*i-1)); k++))
do
echo -n "*"
done
echo
done
Output:
*********
*******
*****
***
*
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 the Case-Esac statement.
echo "Enter two numbers:"
read a b
echo "Choose 1 for Addition, 2 for Subtraction 3 for Multiply and 4 for Divide"
read choice
case $choice in
1) echo "Sum is = $((a+b))" ;;
2) echo "Difference is = $((a-b))" ;;
3) echo "Product is = $((a*b))" ;;
4) echo "Division is = $((a/b))" ;;
*) echo "Invalid Choice"
esac
-------------------- OR --------------------
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!
-------------------- OR --------------------
while true
do
echo "Choose anyOne option"
echo "1.Date"
echo "2.Calendar"
echo "3.Exit"
read choice
case $choice in
1) date ;;
2) cal ;;
3) exit ;;
*) echo "Invalid Choice"
esac
done
Example : Write a shell script or Linux program to create and print the Array values in different ways.
# 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 FIND LARGEST VALUE FROM ARRAY ELEMENTS
arr=(12 45 67 23 89)
largest=${arr[0]}
for i in "${arr[@]}"
do
if [ $i -gt $largest ]
then
largest=$i
fi
done
echo "Largest value is = $largest"
------------ 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
------------ OR -------------
arr[0,0]="A"
arr[0,1]="B"
arr[0,2]="C"
arr[1,0]="D"
arr[1,1]="E"
arr[1,2]="F"
arr[2,0]="G"
arr[2,1]="H"
arr[2,2]="I"
for ((i=0; i<3; i++))
do
for ((j=0; j<3; j++))
do
echo -n "${arr[$i,$j]} "
done
echo
done
Output:
A B C
D E F
G H I
------------ OR -------------
for ((i=0; i<3; i++))
do
for ((j=0; j<3; j++))
do
echo "Enter value for arr[$i,$j]:"
read arr[$i,$j]
done
done
echo "Two-Dimensional Array Elements"
for ((i=0; i<3; i++))
do
for ((j=0; j<3; j++))
do
echo -n "${arr[$i,$j]} "
done
echo
done
Output:
10 20 30
40 50 60
70 80 90
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