Linux Practicles
Linux Practicles
#!/bin/bash
echo $result
}
# Input prompt
read -p "Enter a number to find its factorial: " number
#!/bin/bash
# Input prompt
read -p "Enter the directory path to list files: " directory
3. Write a shell script that accepts a list of file names as its arguments, counts
and reports the occurrence of each word that is present in the first argument file
on other argument files.
#!/bin/bash
4. Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.
#!/bin/bash
# List all files in the current directory with read, write, and execute permissions
for the user
echo "Files in the current directory ($current_dir) with read, write, and execute
permissions for the user:"
5. Write a shell script that deletes all lines containing a specified word in one
or more files supplied as arguments to it.
#!/bin/bash
# Remove lines containing the specified word and save to the temp file
grep -v "$word" "$file" > "$temp_file"
6. Shell script to display the period for which a given user has been working in
the system.
#!/bin/bash
If basic salary is <15000 then HRA =10% of basic and DA =90% of basic
#!/bin/bash
8. Write an awk script to find out total number of books sold in each discipline as
well astotal book sold using associate array down table as given
Electrical 34 electrical 80
Mechanical 67 computers 43
#!/bin/bash
{
# Sum the books sold in each discipline
discipline[$1] += $2;
discipline[$3] += $4;
END {
# Output the results for each discipline
print "Total number of books sold in each discipline:";
for (d in discipline) {
print d ": " discipline[d];
}
9. Create a script file called file properties that reads a file name entered and
output its properties
#!/bin/bash
# Number of links
links=$(ls -l "$filename" | awk '{print $2}')
echo "Number of links: $links"
10. Write a shell script using expr command to read in a string and display a
suitable message if it doesnot have at least 10 characters.
#!/bin/bash
11. Write a shell script that reports the logging in of a specified user within one
minute after he/she logsin. The script automatically terminates if the specified
user does not login during a specified period oftime.
#!/bin/bash
USERNAME=$1
TIMEOUT=$2
CHECK_INTERVAL=1 # Check every second
ELAPSED_TIME=0
# Start monitoring
echo "Monitoring login for user: $USERNAME"
echo "Timeout set for: $TIMEOUT seconds"
echo "User '$USERNAME' did not log in within the specified timeout of $TIMEOUT
seconds."
exit 1