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

Shell_scripts

Uploaded by

shreyansh YEOLE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Shell_scripts

Uploaded by

shreyansh YEOLE
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#!

/bin/bash
# Display logged-in users with headers
echo "USER TTY LOGIN_TIME"
who | awk '{print $1 $2 $3}'

#script 2
ls>ABC;
cat * >> ABC 2>/dev/null
sort ABC
cat ABC.txt

#script 3: sort contents of ABC and save to OABC


#sort ABC > OABC

#script 4: Save the current date and time, number of files & directories in the
current directory, and content of all files into a single file NFL

<<mlc
echo "Date and Time: $(date)">NFL
echo "Number of files and directories: $(ls | wc -l)">>NFL
echo "Contents of Files:">>NFL

cat *>>NFL 2>/dev/null


mlc

#script 5: input a number and test whether it is positive, negative, or 0


<<mlc
read -p "Enter a number: " num1

if [ $num1 -gt 0 ]; then


echo "Positive Number"
elif [$num1 -lt 0]; then
echo "Negative Number"
else
echo "Entered number is Zero"
fi
mlc

#script 6: test whether a filename is a regular file, directory, or other type.

#!/bin/bash
read -p "Enter the filename: " file
if [ -f "$file" ]; then
echo "$file is a regular file."
elif [ -d "$file" ]; then
echo "$file is a directory."
else
echo "$file is of another type."
fi

#script 7
<<mlc
#!/bin/bash
ls -l | grep ^d
mlc

<<mlc
#script 8: Print the greatest of 3 numbers

read -p "Enter three numbers: " a b c


if [ $a -ge $b ] && [ $a -ge $c ]; then
echo "$a is the greatest."
elif [ $b -ge $a ] && [ $b -ge $c ]; then
echo "$b is the greatest."
else
echo "$c is the greatest."
fi
mlc

<<mlc

#script 9: print 12 terms of fibonacci series:

a=0
b=1
echo "Fibonacci Series:"
for ((i=0; i<12; i++)); do
echo -n "$a "
fib=$((a+b))
a=$b
b=$fib
done
echo

mlc
<<mlc
#script 12: Display particular meassages depending on the weekday.
day=$(date +%A)
case $day in
Monday) echo "Start of the work week!";;
Tuesday) echo "Keep pushing forward!";;
Wednesday) echo "Mid-week hustle!";;
Thursday) echo "Almost there!";;
Friday) echo "Weekend is near!";;
Saturday|sunday) echo "Enjoy the weekend!";;
*) echo "Invalid day";;
esac

mlc

<<mlc
#script 13:
day=$(date +%A)
case $day in
Monday|Wednesday) echo "Group 1:Monday and wedesday";;
Tuesday|Thursday) echo "Group 2: Tuesday and Thursday";;
Friday|Saturday) echo "Group 3: Friday and Saturday";;
*) echo "Other day";;
esac
mlc
<<mlc
#script 14: Accept a string from the terminal and echo a message if it doesn't have
at least 9 characters.
read -p "Enter the string: " str
if [ ${#str} -lt 9 ]; then
echo "The string must have at least 9 characters."
fi
mlc
<<mlc
#script 15: find a factorial of a number.
read -p "Enter the number: " num
fact=1;
for((i=1;i<= num; i++)); do
fact=$((fact * i))
done
echo "Factorial of $num is $fact."
mlc

<<mlc
#script 16: swap 2 number
read -p "Enter value of a and b: " x y
echo "Before Swap a = $x and b = $y"
temp=$y
y=$x
x=$temp
echo "After Swap a = $x and b = $y"
mlc
<<mlc
#script 17: Print primes between 1 and 20

for((num=2;num<20;num++)); do
is_prime=1
for ((i=2; i<num; i++)); do
if ((num % i == 0)); then
is_prime=0
break
fi
done
if((is_prime == 1)); then
echo "$num"
fi
done
mlc
<<mlc
#script 18: sort the contents of a file XYZ and save it in BCAII
sort XYZ.txt > BCAII
mlc

#script 19: Display mathematical table of any number.


read -p "Enter the number: " num
for((i=1; i<=10; i++)); do
echo "$num * $i = $((i * num))";
done

You might also like