MIT-Curricular/OS/endsem/bashq.sh
2025-04-11 15:22:22 +05:30

48 lines
1.3 KiB
Bash

#!/bin/bash
# Ask for the directory
echo "Enter directory path:"
read dir
while true; do
echo ""
echo "Menu:"
echo "1. Convert .txt files to .py in $dir"
echo "2. List ownership properties (ls -l) of all files in $dir"
echo "3. Count .sh files and subdirectories in $dir"
echo "4. Exit"
echo -n "Choose an option: "
read option
case $option in
1)
for file in "$dir"/*.txt; do
if [ -f "$file" ]; then
new="${file%.txt}.py"
mv "$file" "$new"
echo "Renamed: $file -> $new"
fi
done
;;
2)
ls -l "$dir"
;;
3)
# Count .sh files in the given directory (errors hidden if none found)
sh_count=$(ls -1 "$dir"/*.sh 2>/dev/null | wc -l)
# Count subdirectories (using a trailing slash pattern)
dir_count=$(ls -d "$dir"/*/ 2>/dev/null | wc -w)
echo ".sh file count: $sh_count"
echo "Subdirectory count: $dir_count"
;;
4)
echo "Exiting..."
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
echo "Bye!"