Shell_scripts
Shell_scripts
/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 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
#!/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
<<mlc
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