0% found this document useful (0 votes)
10 views12 pages

Uxix Question Answere

The document provides an overview of various UNIX commands and concepts, including file attributes, standard input/output, and shell scripting examples for checking prime numbers, palindromes, and Armstrong numbers. It also explains file permissions, environmental variables, process states, and different types of files and shells in UNIX. Additionally, it covers command explanations and usage for managing files and processes.

Uploaded by

r7950541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Uxix Question Answere

The document provides an overview of various UNIX commands and concepts, including file attributes, standard input/output, and shell scripting examples for checking prime numbers, palindromes, and Armstrong numbers. It also explains file permissions, environmental variables, process states, and different types of files and shells in UNIX. Additionally, it covers command explanations and usage for managing files and processes.

Uploaded by

r7950541
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UXIX QUESTION ANSWERE

20. Explain home, path, user, cp, cat in UNIX


 home:
The home directory (~ or /home/username) is the default directory
assigned to a user after login. It stores personal files, configurations,
and user-specific data.
 path:
The PATH is an environment variable that tells the shell which
directories to search for executable files. Example:
/usr/local/bin:/usr/bin:/bin
 user:
A user in UNIX is an account used to log in and execute commands.
Each user has a unique username, UID, and associated permissions.
 cp:
The cp command is used to copy files or directories.
Example: cp file1.txt file2.txt – copies file1 to file2.
 cat:
The cat command is used to display the contents of a file, or
concatenate multiple files.
Example: cat file.txt – shows the contents of file.txt.

21. File attributes in UNIX:


Use ls -l to view file attributes. Example output:
csharp
-rw-r--r-- 1 user group 4096 May 22 10:00 file.txt
 File type and permissions: -rw-r--r--
o First character: file type (- for file, d for directory)
o Next 9 characters: permissions (user, group, others)
 Link count: 1 – number of hard links
 Owner: user – the username who owns the file
 Group: group – the group that owns the file
 Size: 4096 – in bytes
 Modification time: May 22 10:00
 File name: file.txt
22. Explain uniq command in UNIX
 uniq filters out repeated lines in a file, but only if they are adjacent.
Usage:
bash
CopyEdit
sort file.txt | uniq
Options:
 -c: prefix lines by the number of occurrences
 -d: only print duplicate lines
 -u: only print unique lines

23. Standard Input/Output in UNIX


 Standard Input (stdin): Input stream, usually the keyboard. File
descriptor: 0
 Standard Output (stdout): Output stream, usually the screen. File
descriptor: 1
 Standard Error (stderr): For error messages. File descriptor: 2
Examples:
bash
# Redirect output to a file
ls > output.txt

# Redirect error to a file


ls nonexistfile 2> error.txt
# Use input from a file
wc < file.txt

24. Shell Program to Check if a Number is Prime or Not


bash
#!/bin/bash
read -p "Enter a number: " num
if [ $num -le 1 ]; then
echo "Not prime"
exit
fi

for (( i=2; i*i<=$num; i++ ))


do
if [ $((num % i)) -eq 0 ]; then
echo "Not prime"
exit
fi
done

echo "Prime number"

25. Shell Program to Check if a Number is a Palindrome or Not


bash
#!/bin/bash
read -p "Enter a number: " num
rev=0
n=$num

while [ $n -gt 0 ]
do
rem=$((n % 10))
rev=$((rev * 10 + rem))
n=$((n / 10))
done

if [ $rev -eq $num ]; then


echo "$num is a palindrome"
else
echo "$num is not a palindrome"
fi
26. Check Armstrong Number in UNIX (using shell script):
sh
echo "Enter a number:"
read num
sum=0
n=$num

while [ $n -gt 0 ]; do
digit=$((n % 10))
sum=$((sum + digit * digit * digit))
n=$((n / 10))
done

if [ $sum -eq $num ]; then


echo "$num is an Armstrong number."
else
echo "$num is not an Armstrong number."
fi

27. Reverse a Number in UNIX (using shell script)


sh
echo "Enter a number:"
read num
rev=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
rev=$((rev * 10 + digit))
num=$((num / 10))
done
echo "Reversed number: $rev"

28. Check if File is Readable or Writable in UNIX


Sh
echo "Enter filename:"
read file

if [ -r "$file" ]; then
echo "$file is readable"
else
echo "$file is not readable"
fi
if [ -w "$file" ]; then
echo "$file is writable"
else
echo "$file is not writable"
fi

29. Print All Prime Numbers Between 1 to 100 in UNIX


sh
for num in {2..100}; do
is_prime=1
for ((i=2; i*i<=num; i++)); do
if (( num % i == 0 )); then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo $num
fi
done

30. Print Odd Numbers Between 1 to 100 in UNIX


sh
for ((i=1; i<=100; i+=2)); do
echo $i
done

31. Check Leap Year in UNIX (using shell script)


sh
echo "Enter year:"
read year

if (( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )); then


echo "$year is a leap year."
else
echo "$year is not a leap year."
fi

32. Commands Explanation in UNIX:


 nice: Runs a process with a given priority.
 cat: Displays contents of a file.
 test: Evaluates conditional expressions.
 su: Switch user (e.g., su root).
 cp: Copy files or directories.
 chgrp: Change group ownership of a file.
 gzip: Compress files.
 gunzip: Decompress .gz files.
 tar: Archive files (e.g., tar -cvf archive.tar file).

33. Types of Files in UNIX


Run ls -l to view types:
 - : Regular file
 d : Directory
 l : Symbolic link
 c : Character device
 b : Block device
 p : Named pipe
 s : Socket

34. Different Shells in UNIX


Common shells:
 sh – Bourne shell
 bash – Bourne Again SHell
 csh – C shell
 ksh – Korn shell
 zsh – Z shell
 tcsh – Enhanced C shell
Use cat /etc/shells to list available shells.
35. Quoting in UNIX
Quoting is used to prevent the shell from interpreting special
characters.
 Single quotes ': Preserve the literal value of each character within.
bash
echo 'This is $HOME' # Output: This is $HOME
 Double quotes ": Preserve literal value except for $, `, and \.
bash
echo "This is $HOME" # Output: This is /home/username
 Backslash \: Escapes a single character.
bash
echo \$HOME # Output: $HOME

36. Regular Expressions


Used with tools like grep, sed, awk to search patterns:
 . matches any character.
 * matches 0 or more repetitions.
 ^ matches the beginning of a line.
 $ matches the end of a line.
 [abc] matches any one of a, b, or c.
 [^abc] matches any character except a, b, or c.
 \ escapes special characters.

37. File Permissions in UNIX


Every file has permissions for:
 Owner, Group, Others
 Read (r) = 4, Write (w) = 2, Execute (x) = 1
Example:
bash
-rwxr-xr-- # Owner: rwx (7), Group: r-x (5), Others: r-- (4)
Use chmod to change:
bash
chmod 755 filename

38. Environmental Variables in UNIX


Variables that affect shell behavior:
 View: printenv or echo $HOME
 Set: export VAR=value
 Common: $PATH, $HOME, $USER, $SHELL

39. Process States in UNIX


 Running (R): Currently executing.
 Sleeping (S): Waiting for an event.
 Stopped (T): Halted process.
 Zombie (Z): Terminated but not cleaned up.

40. Positional Parameters in UNIX


Used in shell scripts:
 $0: Script name
 $1 to $9: First to ninth arguments
 $@ or $*: All arguments
Example:
bash
#!/bin/bash
echo "First arg: $1"

41. Sticky Bit in UNIX


Used on directories to allow only the owner to delete files.
 Set using:
bash
chmod +t directoryname
 View: Look for t in ls -ld output:
bash
drwxrwxrwt /tmp
42. Background and Foreground Jobs in UNIX
 Foreground: Runs and blocks terminal.
 Background: Runs without blocking terminal.
bash
command & # Run in background
fg # Bring job to foreground
bg # Resume stopped job in background
jobs # List jobs

43. Commands Explanation:


 egrep: Extended grep supporting +, ?, |
 fgrep: Fixed grep, no regex
 mount: Mount file systems
 umask: Set default permission mask
 fsck: File system check tool
 kill: Send signal to a process (e.g., kill -9 PID)
 who: Show who is logged in
 touch: Create empty file or update timestamp

44. Command Explanations


 head: Show first lines (default 10): head filename
 tail: Show last lines: tail filename
 cut: Extract sections from lines:
bash
cut -d':' -f1 /etc/passwd
 du: Disk usage of directories
 df: Disk free space
 cal: Calendar
 bc: Calculator
 date: Show current date/time
 mv: Move/rename files
 rm: Remove files/directories
 cd: Change directory

You might also like