shell script that simulates the file commands rm, cp, cat, mv, cmp, wc, split, and
diff
#!/bin/bash
echo "File Command Simulator"
echo "1. CREATE"
echo "2. DISPLAY"
echo "3. COPY"
echo "4. REMOVE"
echo "5. MOVE"
echo "6. COMPARE"
echo "7. WORD COUNT"
echo "8. SPLIT"
echo "9. DIFFERENCE"
echo "10. EXIT"
echo -n "Enter Your Choice: "
read choice
case $choice in
1)
echo "CREATE COMMAND"
echo -n "Enter Your File Name: "
read filename
vi $filename
echo "File is Created"
;;
2)
echo "DISPLAY COMMAND"
echo -n "Enter the File Name to Display: "
read filename
cat $filename
;;
3)
echo "COPY COMMAND"
echo -n "Enter Source File Name: "
read src
echo -n "Enter Destination File Name: "
read dest
cp $src $dest
echo "File is Copied"
;;
4)
echo "REMOVE COMMAND"
echo -n "Enter the File Name to Remove: "
read filename
rm $filename
echo "File is Removed"
;;
5)
echo "MOVE COMMAND"
echo -n "Enter Source File Name: "
read src
echo -n "Enter Destination Path: "
read dest
mv $src $dest
echo "File is Moved"
;;
6)
echo "COMPARE COMMAND"
echo -n "Enter First File Name: "
read file1
echo -n "Enter Second File Name: "
read file2
cmp $file1 $file2
;;
7)
echo "WORD COUNT COMMAND"
echo -n "Enter the File Name: "
read filename
wc $filename
;;
8)
echo "SPLIT COMMAND"
echo -n "Enter the File Name to Split: "
read filename
echo -n "Enter the Prefix for Split Files: "
read prefix
split $filename $prefix
echo "File is Split"
;;
9)
echo "DIFFERENCE COMMAND"
echo -n "Enter First File Name: "
read file1
echo -n "Enter Second File Name: "
read file2
diff $file1 $file2
;;
10)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid Choice"
;;
Esac
2. Write a shell script to show the following system configuration:
1) whoami and $LOGNAME display the current user and log name.
2) $SHELL, $HOME, uname -o, $PATH, and pwd provide details about the current shell, home directory,
OS type, path settings, and working directory.
3) who | wc -l counts the number of currently logged-in users.
4) cat /etc/shells lists all available shells.
5) lscpu | grep -E 'Model name|CPU MHz' extracts CPU model name and speed.
6) free -h shows memory information in a human-readable format.
Code:
#!/bin/bash
echo "Currently logged user and log name:"
echo "User: $(whoami)"
echo "Log name: $LOGNAME"
echo "Current shell: $SHELL"
echo "Home directory: $HOME"
echo "Operating System type: $(uname -o)"
echo "Current Path setting: $PATH"
echo "Current working directory: $(pwd)"
echo "Number of currently logged users: $(who | wc -l)"
echo "All available shells:"
cat /etc/shells
echo "CPU information:"
lscpu | grep -E 'Model name|CPU MHz'
echo "Memory information:"
free -h
simple shell script that demonstrates the use of pipes, redirection, and the tee command. I’ll also
provide an algorithm to explain the steps.
Algorithm
1. Create a script file: Start by creating a new shell script file.
2. Add the shebang line: This line tells the system which interpreter to use.
3. List files: Use the ls command to list files in the current directory.
4. Filter files: Use grep to filter the list of files.
5. Redirect output: Redirect the output to a file.
6. Use tee command: Use the tee command to display the output and save it to a file
simultaneously.
7. Count lines: Use wc -l to count the number of lines in the output.
Code:
#!/bin/bash
# Step 1: List all files and directories
echo "Listing all files and directories:"
ls
# Step 2: Filter the list to show only .txt files
echo "Filtering to show only .txt files:"
ls | grep '\.txt$'
# Step 3: Redirect the filtered list to a file
echo "Saving the filtered list to filtered_files.txt"
ls | grep '\.txt$' > filtered_files.txt
# Step 4: Use tee to display and save the output simultaneously
echo "Using tee to display and save the output to tee_output.txt"
ls | grep '\.txt$' | tee tee_output.txt
# Step 5: Count the number of .txt files and display the count
echo "Counting the number of .txt files:"
count=$(ls | grep '\.txt$' | wc -l)
echo "Number of .txt files: $count"
shell script that displays the current date, user name, file listing, and directories based on the user’s
choice.
Algorithm:
1. Menu Display: The script first displays a menu with options for the user to choose from.
2. User Input: It then reads the user’s choice.
3. Choice Processing: Based on the user’s choice, it executes the corresponding command:
o Option 1: Displays the current date and time.
o Option 2: Displays the user name.
o Option 3: Lists files (excluding directories).
o Option 4: Lists directories.
o Option 5: Exits the script.
o Invalid Choice: Prompts the user to run the script again with a valid option.
Code:
#!/bin/bash
# Display menu options
echo "Please choose an option:"
echo "1. Display current date"
echo "2. Display user name"
echo "3. Display file listing"
echo "4. Display directories"
echo "5. Exit"
# Read user choice
read -p "Enter your choice [1-5]: " choice
# Process user choice
case $choice in
1)
echo "Current date and time: $(date)"
;;
2)
echo "User name: $USER"
;;
3)
echo "File listing:"
ls -p | grep -v /
;;
4)
echo "Directories:"
ls -d */
;;
5)
echo "Exiting..."
exit 0
;;
*)
echo "Invalid choice. Please run the script again and choose a valid option."
;;
Esac
Shell script that checks whether a given string is a palindrome.
Algorithm:
1. Input: Prompt the user to enter a string.
2. Reverse: Reverse the entered string using the rev command.
3. Compare: Compare the original string with the reversed string.
4. Output: Print whether the string is a palindrome or not.
Code
#!/bin/bash
# Prompt the user to enter a string
read -p "Enter a string: " input_string
# Reverse the string
reversed_string=$(echo "$input_string" | rev)
# Check if the original string and the reversed string are the same
if [ "$input_string" == "$reversed_string" ]; then
echo "The string '$input_string' is a palindrome."
else
echo "The string '$input_string' is not a palindrome."
Fi
Java program and algorithm that prompts the user for an integer and then prints out
all the prime numbers up to that Integer
Algorithm:
1. Input: Prompt the user to enter an integer.
2. Initialization: Create a boolean variable isPrime to track if a number is prime.
3. Outer Loop: Iterate from 2 to the input integer.
4. Inner Loop: Iterate from 2 to the square root of the current number.
5. Check Divisibility: If the current number is divisible by any number in the inner loop, set isPrime
to false and break.
6. Print Prime: If isPrime is still true after the inner loop, print the current number as a prime.
import java.util.Scanner;
public class PrimeNumbers
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println("Prime numbers up to " + n + ":");
for (int i = 2; i <= n; i++)
boolean isPrime = true;
for (int j = 2; j * j <= i; j++)
if (i % j == 0)
isPrime = false;
break;
if (isPrime)
{ System.out.print(i + " ");