10 Shell Scripting Programs
(Intermediate)
1. Check Strong Password
read -p "Enter password: " pass
if [[ ${#pass} -ge 8 && "$pass" =~ [0-9] && "$pass" =~ [@#$%] ]]
then
echo "Strong Password"
else
echo "Weak Password"
fi
2. Count Hidden Files
count=$(ls -d .* 2>/dev/null | wc -l)
echo "Hidden files: $count"
3. Second Largest Number
echo "Enter numbers:"
read -a arr
sorted=($(printf '%s
' "${arr[@]}" | sort -nr))
echo "Second largest: ${sorted[1]}
4. Rename .txt to .bak
for file in *.txt
do
mv "$file" "${file%.txt}.bak"
done
5. Count Logged-in Users
who | wc -l
6. Check File Permissions
read -p "Enter file name: " file
if [ -r "$file" ]; then echo "Readable"; fi
if [ -w "$file" ]; then echo "Writable"; fi
if [ -x "$file" ]; then echo "Executable"; fi
7. Reverse a Number
read -p "Enter number: " num
rev=0
while [ $num -gt 0 ]
do
rem=$((num % 10))
rev=$((rev * 10 + rem))
num=$((num / 10))
done
echo "Reversed: $rev"
8. Display Calendar
read -p "Enter month: " m
read -p "Enter year: " y
cal $m $y
9. Largest File in Directory
ls -S | head -n 1
10. Palindrome String
read -p "Enter string: " str
rev=$(echo $str | rev)
if [ "$str" = "$rev" ]
then
echo "Palindrome"
else
echo "Not Palindrome"
fi