#!
/bin/bash
# This script displays information about currently logged-in users using the 'who' command.
echo "--- Currently Logged-in Users ---"
# Execute the 'who' command to show basic user information
who
echo ""
echo "--- Detailed User Information (who -a) ---"
# Execute 'who -a' for a more comprehensive display
who -a
echo ""
echo "--- Your Login Information (who am i) ---"
# Execute 'who am i' to show details about your current session
who am i
#!/bin/bash
# This script demonstrates various list commands in a shell environment.
echo "--- Listing contents of the current directory (ls) ---"
ls
echo -e "\n--- Listing contents with long format (ls -l) ---"
ls -l
echo -e "\n--- Listing all files, including hidden ones (ls -a) ---"
ls -a
echo -e "\n--- Listing files in a specific directory (e.g., /home) ---"
# Replace '/home' with a directory path relevant to your system
ls /home
echo -e "\n--- Listing processes (ps aux) ---"
ps aux
echo -e "\n--- Listing network connections (netstat -tuln) ---"
netstat -tuln
echo -e "\n--- Listing command history (history) ---"
history 5 # Shows the last 5 commands
#!/bin/bash
# Define the input file (optional, can also use pipes for input)
INPUT_FILE="data.txt"
# Create a sample data file if it doesn't exist
if [ ! -f "$INPUT_FILE" ]; then
echo "apple" > "$INPUT_FILE"
echo "banana" >> "$INPUT_FILE"
echo "grape" >> "$INPUT_FILE"
echo "orange" >> "$INPUT_FILE"
echo "kiwi" >> "$INPUT_FILE"
echo "10" >> "$INPUT_FILE"
echo "2" >> "$INPUT_FILE"
echo "5" >> "$INPUT_FILE"
fi
echo "Original content of $INPUT_FILE:"
cat "$INPUT_FILE"
echo ""
# Sort alphabetically in ascending order (default behavior)
echo "Sorted alphabetically (ascending):"
sort "$INPUT_FILE"
echo ""
# Sort alphabetically in reverse (descending) order
echo "Sorted alphabetically (descending):"
sort -r "$INPUT_FILE"
echo ""
# Sort numerically
echo "Sorted numerically:"
sort -n "$INPUT_FILE"
echo ""
# Sort numerically in reverse order
echo "Sorted numerically (descending):"
sort -nr "$INPUT_FILE"
echo ""
# Sort and remove duplicate lines
echo "Sorted with unique lines:"
sort -u "$INPUT_FILE"
echo ""
# Sort based on a specific column (e.g., assuming space-separated data)
# Create a new file for column sorting demonstration
echo "column1 data1" > "column_data.txt"
echo "column2 data3" >> "column_data.txt"
echo "column3 data2" >> "column_data.txt"
echo "Original content of column_data.txt:"
cat "column_data.txt"
echo ""
echo "Sorted by the second column:"
sort -k 2 "column_data.txt"
echo ""
# Clean up the temporary files
rm -f "$INPUT_FILE" "column_data.txt"
#!/bin/bash
# Prompt the user for a filename
echo "Enter the filename:"
read filename
# Check if the file exists
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' not found."
exit 1
fi
# Count lines, words, and characters using wc
echo "--- Counts for '$filename' ---"
wc -l "$filename" # Count lines
wc -w "$filename" # Count words
wc -c "$filename" # Count characters
#!/bin/bash
# Shell script demonstrating cat command usage
echo "==== CAT DEMO ===="
# 1. Display the contents of an existing file
echo "Displaying contents of file1.txt:"
cat file1.txt
# 2. Create a new file with content using here-doc and cat
echo "Creating file2.txt..."
cat << EOF > file2.txt
This is file2.
Created using a shell script and cat command.
EOF
echo "Contents of file2.txt:"
cat file2.txt
# 3. Append content to file2.txt
echo "Appending to file2.txt..."
cat << EOF >> file2.txt
This line is appended.
EOF
echo "Contents of file2.txt after appending:"
cat file2.txt
# 4. Concatenate multiple files
echo "Concatenating file1.txt and file2.txt into combined.txt..."
cat file1.txt file2.txt > combined.txt
echo "Contents of combined.txt:"
cat combined.txt
echo "==== END OF DEMO ===="
#!/bin/bash
# Prompt the user to enter three numbers
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
echo "Enter the third number:"
read num3
# Initialize 'smallest' with the first number
smallest=$num1
# Compare 'smallest' with the second number
if [ "$num2" -lt "$smallest" ]; then
smallest=$num2
fi
# Compare 'smallest' with the third number
if [ "$num3" -lt "$smallest" ]; then
smallest=$num3
fi
# Print the smallest number
echo "The smallest number is: $smallest"
#!/bin/bash
# Prompt the user to enter a number
echo "Enter a non-negative integer:"
read num
# Validate input: Check if the number is non-negative
if (( num < 0 )); then
echo "Factorial is not defined for negative numbers."
exit 1
fi
# Initialize factorial to 1 (0! and 1! are both 1)
factorial=1
# Calculate factorial using a for loop
for (( i=2; i<=num; i++ )); do
factorial=$(( factorial * i ))
done
# Display the result
echo "The factorial of $num is: $factorial"
#!/bin/bash
# Prompt the user to enter a number
echo "Enter the number for which you want the multiplication table:"
read num
# Display the multiplication table
echo "Multiplication table for $num:"
for i in {1..10}
do
# Calculate the product and display the result
product=$((num * i))
echo "$num * $i = $product"
done
#!/bin/bash
echo "Enter your favorite color (red, blue, green, or other):"
read color
case "$color" in
"red")
echo "Red is a vibrant color!"
;;
"blue")
echo "Blue is often associated with calmness."
;;
"green")
echo "Green represents nature and growth."
;;
*) # Default case - matches anything not explicitly handled above
echo "That's an interesting color choice!"
;;
esac
echo "End of color analysis."
#!/bin/bash
# Function to display the current username and present working directory
show_user_and_pwd() {
echo "Current User: $USER"
echo "Present Working Directory: $(pwd)"
}
# Call the function
show_user_and_pwd
#!/bin/bash
# Function to calculate age from a set birth date
calculate_age() {
echo "Enter your birth date (YYYY-MM-DD):"
read birth_date
# Convert birth date and current date to seconds since Unix epoch
birth_sec=$(date -d "$birth_date" +%s)
current_sec=$(date +%s)
# Calculate the difference in seconds, then convert to years
age=$(( (current_sec - birth_sec) / (365 * 24 * 60 * 60) ))
echo "You are approximately $age years old."
}
# Call the function
calculate_age
#!/bin/bash
# Prompt user to enter a path
read -p "Enter the path to check: " path
# Check if the input is empty
if [ -z "$path" ]; then
echo "No path entered. Exiting."
exit 1
fi
# Check if the path is a directory
if [ -d "$path" ]; then
echo "'$path' is a directory."
else
echo "'$path' is NOT a directory."
fi
#!/bin/bash
# Ask the user to enter a lowercase string
read -p "Enter a lowercase string: " input
# Convert to uppercase using tr
echo "$input" | tr 'a-z' 'A-Z'
#!/bin/bash
echo -n "Enter a number: "
read num
if (( $num % 2 == 0 )); then
echo "$num is an even number."
else
echo "$num is an odd number."
fi