Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Function to check if a file exists
- check_file_exists() {
- if [ -f "$1" ]; then
- echo "File '$1' exists."
- else
- echo "File '$1' does not exist."
- fi
- }
- # Function to count the number of lines in a file
- count_lines() {
- local lines=$(wc -l < "$1")
- echo "Number of lines in '$1': $lines"
- }
- # Function to create a new file
- create_file() {
- echo "Enter text to write into the file (press CTRL+D to save):"
- cat > "$1"
- echo "File '$1' created with the entered text."
- }
- # Main menu
- while true; do
- echo "===== Bash Script Menu ====="
- echo "1. Check if a file exists"
- echo "2. Count the number of lines in a file"
- echo "3. Create a new file"
- echo "4. Exit"
- read -p "Enter your choice [1-4]: " choice
- case $choice in
- 1)
- read -p "Enter the filename to check: " filename
- check_file_exists "$filename"
- ;;
- 2)
- read -p "Enter the filename to count lines: " filename
- count_lines "$filename"
- ;;
- 3)
- read -p "Enter the filename to create: " filename
- create_file "$filename"
- ;;
- 4)
- echo "Exiting the script. Goodbye!"
- exit 0
- ;;
- *)
- echo "Invalid choice. Please enter a valid option [1-4]."
- ;;
- esac
- echo
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement