Q1. Explain in detail the common commands used in Linux.
1. Directory Management Commands:
- ls: List directory contents.
- cd: Change current directory.
- pwd: Print working directory.
- mkdir: Create a new directory.
- rmdir: Remove an empty directory.
2. File Management Commands:
- cat: Concatenate and display file contents.
- chmod: Change file permissions.
- cp: Copy files or directories.
- mv: Move or rename files or directories.
- rm: Remove files or directories.
3. General Purpose Commands:
- wc: Word, line, and byte count of files.
- cal: Display a calendar.
- date: Show or set the system date and time.
- who: Show who is logged in.
- tty: Print the file name of the terminal.
- ln: Create hard or symbolic links.
Q2. Use the Linux Commands to match each situation:
1. All three character filenames:
ls ???
2. Filenames containing 'a' or 'b' or 'c':
ls *[abc]*
3. Filenames beginning with a particular string (e.g., 'file'):
ls file*
4. Filenames beginning with 'ca' and ending with two digits:
ls ca[0-9][0-9]
5. Filenames beginning with 's' and having 'a' somewhere:
ls s*[a]*
Q3. Shell script to find Factorial of a number
#!/bin/bash
read -p "Enter a number: " num
fact=1
for (( i=1; i<=num; i++ )); do
fact=$((fact * i))
done
echo "Factorial of $num is $fact"
Q4. Shell script to calculate Fibonacci series
#!/bin/bash
read -p "Enter number of terms: " n
a=0; b=1
echo "Fibonacci series:"
for (( i=0; i<n; i++ )); do
echo -n "$a "
fn=$((a + b))
a=$b; b=$fn
done
echo
Q5. Shell script to find the greatest of three numbers
#!/bin/bash
read -p "Enter three numbers: " a b c
if (( a>=b && a>=c )); then
max=$a
elif (( b>=a && b>=c )); then
max=$b
else
max=$c
fi
echo "Greatest is $max"
Q6. Shell script that displays lines between given line numbers
#!/bin/bash
file=$1; start=$2; end=$3
sed -n "${start},${end}p" "$file"
Q7. Shell script to list files with r, w, x permissions
#!/bin/bash
echo "Files with read, write, and execute permissions:"
for f in *; do
if [[ -f $f && -r $f && -w $f && -x $f ]]; then
echo "$f"
fi
done
Q8. Redirect stdin and stdout so scanf() reads from pipe and printf() writes into pipe
Example using unnamed pipe:
echo "input_data" | ./consumer_program > output.txt
Example using named pipe:
mkfifo mypipe
echo "input_data" > mypipe &
./consumer_program < mypipe
Q9. Script to check even or odd (run in Vim with :!bash)
#!/bin/bash
read -p "Enter a number: " num
if (( num % 2 == 0 )); then
echo "Even"
else
echo "Odd"
fi
In Vim: :!bash even_odd.sh
Q10. Script to print multiplication tables between two numbers
#!/bin/bash
read -p "Enter start number: " start
read -p "Enter end number: " end
for (( i=start; i<=end; i++ )); do
echo "Table of $i:"
for (( j=1; j<=10; j++ )); do
echo "$i x $j = $((i*j))"
done
echo
done
In Vim: :!bash multiply.sh
Q11. Script to search a string and display it using shell programming
#!/bin/bash
read -p "Enter filename: " file
read -p "Enter search string: " str
grep -n "$str" "$file"
In Vim: :!bash search_string.sh