0% found this document useful (0 votes)
23 views

shell_script-programs(1)

Uploaded by

Nalina V
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

shell_script-programs(1)

Uploaded by

Nalina V
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1)Write a Simple script to read user input and perform addition operations

echo -n "Enter number 1 : " # -n option supresses newline

read NUM1 # Read the user input from Standard Input and store in Variable NUM1

echo -n "Enter number 2 : "

read NUM2

SUM=$(($NUM1 + $NUM2)) # Arithmetic expansion using double parentheses

echo "The sum is $SUM"

SUM=`expr $NUM1 + $NUM2` # Arithmetic expansion using backticks.

#Usage of expr command to evaluate the expression

echo "The sum is $SUM"

2) Write a Shell script program to print today’s date, calendar & type of shell.

#! /bin/bash

echo "Today's date : `date`"

echo "this month calender: `cal`"

echo "My Shell : $SHELL"

3) Write a Shell script program to print path and current working directory.

4) Write a shell script program to demonstrate the working of constant variable.


#!/bin/bash

# Declare a constant variable

readonly CONSTANT_VAR="Hello, World!"

# Display the constant variable

echo "The value of CONSTANT_VAR is: $CONSTANT_VAR"

# Try to modify the constant variable (this will generate an error)

echo "Attempting to change the value of CONSTANT_VAR..."

CONSTANT_VAR="New Value"

# Display the constant variable again

echo "The value of CONSTANT_VAR is still: $CONSTANT_VAR"

5) Write a shell script program to print number from 1 to 10 using until loop

#!/bin/bash

# Initialize the counter variable

counter=1

# While loop to print numbers from 1 to 10

while [ $counter -le 10 ];


do

echo $counter

counter=$((counter + 1)) # Increment the counter

done

Until loop

#!/bin/bash #

Initialize the variable

num=1

# Use until loop to print numbers from 1 to 10

until [ $num -gt 10 ]

do

echo $num

num=$((num + 1))

done

6) Write a shell script program to print the Fibonacci series of the term.

#!/bin/bash

# Prompt the user to enter the number of terms for the Fibonacci series

read -p "Enter the number of terms: " n

# Check if the input is valid (positive integer)

if [ $n -le 0 ]; then
echo "Please enter a positive integer."

exit 1 # Exit the script if the input is invalid

fi

# Initialize the first two terms of the Fibonacci series

a=0 # First term

b=1 # Second term

echo "Fibonacci series up to $n terms:"

# Loop to generate the Fibonacci series

for (( i=1; i<=n; i++ ))

do

# Print the current term

echo -n "$a " # -n ensures the numbers are printed on the same line

# Calculate the next term in the series

fib=$((a + b)) # Add the previous two terms

a=$b # Update `a` to the value of `b`

b=$fib # Update `b` to the value of the new term

done

# Print a newline for better formatting

echo
7) Write a shell script program to print numbers from 1 to 100 using while loop.

#!/bin/bash

# Initialize the counter

num=1

echo "Numbers from 1 to 100:"

# Loop to print numbers from 1 to 100

while [ $num -le 100 ]; do

echo $num

num=$((num + 1)) # Increment the counter

done

8) Write a shell script to find the factorial of given integer.

#!/bin/bash

# Prompt the user for an integer

read -p "Enter a positive integer: " num

# Validate input

if [[ $num -lt 0 ]]; then

echo "Factorial is not defined for negative numbers."

exit 1
fi

# Initialize variables

factorial=1

i=1

# Calculate factorial using a while loop

while [ $i -le $num ]; do

factorial=$((factorial * i))

i=$((i + 1))

done

# Output the result

echo "The factorial of $num is: $factorial"

9) Write a shell script that displays a list of all the files in the current directory

#!/bin/bash

# Display a message

echo "Listing all files in the current directory:"

# Use the 'ls' command to list files

ls -l
10) Write a shell script called hello which outputs the following:

• your username

• the time and date

• who is logged on

• also output a line of asterisks (*********) after each section.

#!/bin/bash

# Display the username

echo "Your username:"

whoami

echo "*********"

# Display the current time and date

echo "The current time and date:"

date

echo "*********"

# Display who is logged on

echo "Who is logged on:"


who

echo "*********"

11) Write the shell script program to demonstrate the working of command line arguments
such as $0, $#, $* & $?.

#!/bin/bash

# Display the name of the script (stored in $0)

echo "Script name: $0"

# Display the total number of arguments passed (stored in $#)

echo "Number of arguments passed: $#"

# Display all the arguments as a single string (stored in $*)

echo "All arguments as a single string: $*"

# Demonstrate the exit status of the last command (stored in $?)

echo "Running a sample command (ls)..."

ls # Run `ls`

echo "Exit status of the last command: $?"

12) Write a shell script to echo the string length of the given string as argument.

#!/bin/bash

# Check if a string argument is provided


if [ $# -eq 0 ]; then

echo "Usage: $0 <string>"

exit 1

fi

# Store the input string

input_string="$1"

# Calculate the length of the string

string_length=${#input_string}

# Print the length of the string

echo "The length of the string '$input_string' is: $string_length"


13) Write a shell script to combine any three text files into a singorder as they appear in the
arguments) and display the word count.

#!/bin/bash

# Check if exactly three file arguments are provided

if [ $# -ne 3 ]; then

echo "Usage: $0 <file1> <file2> <file3>"

exit 1

fi

# Assign the arguments to variables for clarity

file1="$1"

file2="$2"

file3="$3"

# Combined output file

output_file="combined_file.txt"

# Combine the files into a single file

cat "$file1" "$file2" "$file3" > "$output_file"

# Check if the combination was successful


if [ $? -ne 0 ]; then

echo "Error: Failed to combine files."

exit 1

fi

# Display the word count of the combined file

word_count=$(wc -w < "$output_file")

echo "The combined file '$output_file' contains $word_count words."

# Optionally, display the content of the combined file (uncomment the line below)

# cat "$output_file"

14) Write a shell script program to print numbers from 1 to 100 in reverse order using for
loop

#!/bin/bash

# Print numbers from 100 to 1 in reverse order

for (( i=100; i>=1; i-- ))

do

echo $i

done
15) Write shell script program to print even numbers from 1 to 100 using for loop

16) Write shell script program to print environment variables / predefined variables.

You might also like