0% found this document useful (0 votes)
36 views15 pages

Coding Samples

The document contains a series of shell scripting problems and their solutions, covering various tasks such as listing files, backing up files, changing directories, and managing user permissions. Each problem statement includes input and output formats, code constraints, and a sample solution in shell script format. The tasks are designed to be executed in a Unix/Linux environment and address common file and system management operations.

Uploaded by

harsainyamsingh6
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)
36 views15 pages

Coding Samples

The document contains a series of shell scripting problems and their solutions, covering various tasks such as listing files, backing up files, changing directories, and managing user permissions. Each problem statement includes input and output formats, code constraints, and a sample solution in shell script format. The tasks are designed to be executed in a Unix/Linux environment and address common file and system management operations.

Uploaded by

harsainyamsingh6
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

Sample Coding Question

1. Problem Statement Write a shell script that lists all files and directories in the current
directory.
Input Format: No input required; operates in the current directory.
Output Format: Displays the names of all files and directories.
Code Constraints:
• Use the ls command.
• The script should work in a Unix/Linux environment.
Solution:
#!/bin/bash
ls
Sample Testcase 1 Initial Files in current directory: documents [Link]
Output: documents [Link]

2. Problem Statement
Write a shell script that backs up all .txt files from the /home/user/documents/ directory to
/home/user/backup/. The script should ensure the backup directory exists before copying the
files and should append the current date to each filename.

Input Format:
The script does not take user input; it operates on files in the /home/user/documents/
directory.

Output Format:
The script creates backup files in /home/user/backup/ with the format filename_YYYY-MM-
[Link].
Code Constraints:
 The script should work in a Unix/Linux environment.
 The backup directory should be created if it does not exist.
 The current date should be appended in YYYY-MM-DD format to each backed-up file.
Solution: (in Shell Script)
#!/bin/bash
mkdir -p /home/user/backup
for file in /home/user/documents/*.txt; do
cp "$file" "/home/user/backup/$(basename "$file")_$(date +%Y%m%d)"
done
Sample Testcase 1
Initial Files in /home/user/documents/:
[Link]
[Link]
Output Files in /home/user/backup/ (assuming today's date is 2025-03-03):
notes_2025-[Link]
report_2025-[Link]

3. Problem Statement Write a shell script that changes the current directory to /var/log and lists all log
files.

Input Format: No input required.


Output Format: Displays a list of files in /var/log.
Code Constraints:
• Use the cd and ls commands.
• Handle errors if the directory does not exist.
Solution:
#!/bin/bash
cd /var/log || { echo "Directory not found"; exit 1; }
ls
Sample Testcase 1 Initial Directory: /
Output: [Link] syslog

4. Problem Statement Write a shell script that displays the full path of the current working directory.
Input Format: No input required.
Output Format: Prints the absolute path.
Code Constraints:
• Use the pwd command.
Solution:
#!/bin/bash
pwd
Sample Testcase 1 Current Directory: /home/user/projects
Expected Output: /home/user/projects

5. Problem Statement : Write a shell script that copies a file named [Link] from the current directory
to /tmp.

Input Format: No user input required.


Output Format: File is copied to /tmp directory.
Code Constraints:
• Use the cp command.
Solution:
#!/bin/bash
cp [Link] /tmp/
Sample Testcase 1 Initial Files in current directory: [Link]
Expected Files in /tmp: [Link]

6. Problem Statement
Write a shell script that moves a file named [Link] from /tmp to the current directory.
Input Format:No user input.
Output Format:File is moved to the current directory.
Code Constraints:
• Use the mv command.
Solution:
#!/bin/bash
mv /tmp/[Link] .
Sample Testcase 1
Initial Files in /tmp:
[Link]
Expected Files in current directory:
[Link]

7. Problem Statement Write a shell script that appends the current date and time to a log file called
[Link].

Input Format: No user input.

Output Format: Date and time appended to the log file.

Code Constraints:

• Use >> redirection operator.

Solution:

#!/bin/bash

date >> [Link]

Sample Testcase 1 Expected Output in [Link] (example): Sat Apr 20 [Link] UTC 2025

8. Problem Statement Write a shell script that redirects the output of the ls command to a file named
file_list.txt.

Input Format: No input.

Output Format: Output saved in file_list.txt

Code Constraints:
• Use > redirection operator.

Solution:

#!/bin/bash

ls > file_list.txt

Sample Testcase 1 Initial Files: [Link]

Expected file_list.txt content: [Link]

9. Problem Statement

Write a shell script to delete a directory named old_folder from the current directory.

Input Format: No user input.

Output Format: The directory is deleted.

Code Constraints:

• Use rmdir, assuming the directory is empty.

Solution:

#!/bin/bash

rmdir old_folder

Sample Testcase 1

Initial Directories:

old_folder (empty)

Expected State after execution:

old_folder removed

10. Problem Statement

Write a shell script to create a directory named new folder in the current directory.

Input Format: No user input.

Output Format: A directory named new folder is created.

Code Constraints:

• Use the mkdir command.

Solution:
#!/bin/bash

mkdir new folder

Sample Testcase 1

Initial State:

No new folder present

Expected State after execution:

new_folder directory created

11. Problem Statement

Write a shell script that removes all .bak files in the current directory.

Input Format:No user input.

Output Format:All .bak files are deleted.

Code Constraints:

• Use the rm command with wildcard.

Solution:

#!/bin/bash

rm -f *.bak

Sample Testcase 1Initial Files in current directory:

[Link]

[Link]

[Link]

Expected Files after execution:

[Link]

[Link] Statement

Write a shell script that counts the total number of lines across all .sh files in the /scripts directory and
prints the sum.

Input Format:

The script does not take user input; it operates on .sh files in the /scripts directory.

Output Format:
The script prints the total number of lines across all .sh files in the /scripts directory.

Code Constraints:

 The script should work in a Unix/Linux environment.

 If there are no .sh files in the /scripts directory, the output should be 0.

 The script should handle cases where files contain spaces in their names.

Solution: (in Shell Script)

#!/bin/bash

total=0

for file in /scripts/*.sh; do

total=$((total + $(wc -l < "$file")))

done

echo "Total lines: $total"

Sample Testcase 1

Initial Files in /scripts/:

plaintext

CopyEdit

[Link] (5 lines)

[Link] (8 lines)

[Link] (10 lines)

Output:

plaintext

CopyEdit

23

Sample Testcase 2

Initial Files in /scripts/:


No .sh files present.

Output:

plaintext
CopyEdit

13. Problem Statement Write a shell script that counts how many .sh files are in the current directory.

Input Format: No user input.

Output Format: Total count of .sh files.

Code Constraints:

• Use ls and wc -l.

Solution:

#!/bin/bash

ls *.sh 2>/dev/null | wc -l

Sample Testcase 1 Files: [Link] [Link] [Link]

Expected Output: 2

13. Problem Statement Write a shell script to display the size of each .log file in the current directory.

Input Format: No user input.

Output Format: File names with sizes.

Code Constraints:

• Use du -h.

Solution:

#!/bin/bash

du -h *.log

Sample Testcase 1 Files: [Link] (512K) [Link] (1.1M)

Expected Output: [Link] 512K [Link] 1.1M

14. Problem Statement Write a shell script that reads user input and writes it to a file called
user_input.txt.

Input Format: User types a string.


Output Format: String saved in user_input.txt

Code Constraints:

• Use read and > operator.

Solution:

#!/bin/bash

echo "Enter text:"

read input

echo "$input" > user_input.txt

Sample Testcase 1 User Input: Learning Linux

Expected Output: user_input.txt contains "Learning Linux"

15. Problem Statement Write a shell script that creates a backup of all .txt files from
/home/user/documents to /home/user/backup with date appended.

Input Format: No input; operates on files in /home/user/documents.

Output Format: Files created in /home/user/backup as filename_YYYY-[Link].

Code Constraints:

• Ensure backup directory exists.

• Append date to filename.

Solution:

#!/bin/bash

mkdir -p /home/user/backup

for file in /home/user/documents/*.txt; do

cp "$file" "/home/user/backup/$(basename "$file" .txt)_$(date +%Y-%m-%d).txt"

done

Sample Testcase 1 Initial Files in /home/user/documents/: [Link] [Link]

Output Files in /home/user/backup/ (assuming today's date is 2025-03-03): notes_2025-[Link]


report_2025-[Link]

16. Problem Statement Write a shell script to display the number of words in all .txt files in the current
directory.
Input Format: No user input.

Output Format: Total word count.

Code Constraints:

• Use wc -w.

Solution:

#!/bin/bash

cat *.txt | wc -w

Sample Testcase 1 Files: [Link] (5 words) [Link] (3 words)

Expected Output: 8

17. Sample Coding Question

Problem Statement
Write a shell script that extracts lines containing the word "ERROR" from the /var/log/syslog file and
saves them to error_logs.txt. The script should ensure that the error_logs.txt file is created or
overwritten each time the script runs.

Input Format:
The script does not take user input; it operates on the /var/log/syslog file.

Output Format:
The script creates a file named error_logs.txt, containing all lines from /var/log/syslog that include the
word "ERROR".

Code Constraints:

 The script should work in a Unix/Linux environment.

 It should extract only lines containing the exact word "ERROR" (case-sensitive).

 The output file should be saved in the current working directory.

Solution: (in Shell Script)

#!/bin/bash

grep "ERROR" /var/log/syslog > error_logs.txt

Sample Testcase 1
Initial Content in /var/log/syslog:

Mar 03 [Link] server kernel: INFO: System boot complete

Mar 03 [Link] server app: ERROR: Failed to start service


Mar 03 [Link] server daemon: WARNING: Low disk space

Mar 03 [Link] server app: ERROR: Connection timed out

Output File (error_logs.txt):

Mar 03 [Link] server app: ERROR: Failed to start service

Mar 03 [Link] server app: ERROR: Connection timed out

18. Problem Statement Write a shell script that sets read, write, and execute permissions for the owner
only on all .sh files in /home/scripts/.

Input Format: The script targets all .sh files.

Output Format: Only the owner has read/write/execute permission (chmod 700).

Code Constraints:

• Works in a Unix/Linux environment.

Solution:

#!/bin/bash

chmod 700 /home/scripts/*.sh

Sample Testcase 1: Initial Files in /home/scripts/: [Link] - permissions: -rwxr-xr-x [Link] -


permissions: -rw-r--r--

Output Files in /home/scripts/: [Link] - permissions: -rwx------ [Link] - permissions: -rwx------

19. Problem Statement Write a shell script that changes the ownership of all files in /project/data/ to
user "manager" and group "staff".

Input Format: Static path used in script.

Output Format: Ownership of all files in /project/data/ is updated to manager:staff.

Code Constraints:

• Script should recursively change ownership.

Solution:

#!/bin/bash

chown -R manager:staff /project/data/

Sample Testcase 1: Initial Files in /project/data/: [Link] - owner: root:root [Link] - owner:
root:root
Output Files in /project/data/: [Link] - owner: manager:staff [Link] - owner: manager:staff

20. Problem Statement Write a shell script that creates a group named "analytics" and assigns an
existing user "datauser" to this group.

Input Format: No user input required.

Output Format: Group "analytics" is created and "datauser" is added to it.

Code Constraints: • Script should work in a Unix/Linux environment. • Should not error if the group
already exists.

Solution:

#!/bin/bash

groupadd analytics 2>/dev/null

usermod -aG analytics datauser

Sample Testcase 1: Initial System State:

 User "datauser" exists

 Group "analytics" does not exist

Output:

 Group "analytics" created

 "datauser" is now part of "analytics"

21. Problem Statement Write a shell script to delete a user named "tempuser" and remove their home
directory.

Input Format: No user input; the script targets "tempuser" specifically.

Output Format: User "tempuser" is deleted and their home directory is removed.

Code Constraints:

• The script should work in a Unix/Linux environment.

• It should not throw an error if the user doesn't exist.

Solution:

#!/bin/bash

deluser --remove-home tempuser 2>/dev/null

Sample Testcase 1: Initial System State:


 User "tempuser" exists

 Home directory: /home/tempuser

Output:

 User "tempuser" deleted

 Directory /home/tempuser removed

22. Problem Statement Write a shell script that creates a new user named "projectuser" and adds the
user to a group called "devteam".

Input Format: The script does not take user input; it creates a specific user and group.

Output Format: A new user "projectuser" is created and added to the group "devteam".

Code Constraints:

• The script should work in a Unix/Linux environment.

• It should check if the group exists before adding the user.

Solution: (in Shell Script)

#!/bin/bash

groupadd devteam 2>/dev/null

adduser --disabled-password --gecos "" projectuser

usermod -aG devteam projectuser

Sample Testcase 1: Initial System State:

 No user named "projectuser"

 No group named "devteam"

Output:

 User "projectuser" is created

 Group "devteam" is created

 User "projectuser" is added to "devteam"

23. Problem Statement


Write a shell script that continuously checks the CPU load every 5 seconds and logs the output into a file
named [Link]. The script should run indefinitely and append each entry with a timestamp.
Input Format:
The script does not take user input; it continuously monitors the system's CPU load.

Output Format:
The script appends CPU load values with timestamps to [Link] in the format:
YYYY-MM-DD HH:MM:SS CPU Load: <value>

Code Constraints:

 The script should work in a Unix/Linux environment.

 It should log the CPU load every 5 seconds.

 It should append logs rather than overwriting them.

Solution: (in Shell Script)

#!/bin/bash

while true; do

uptime >> [Link]

sleep 5

done

Sample Testcase 1
Initial State:
No [Link] file exists.

After Running the Script for 10 Seconds:


Contents of [Link] (assuming sample CPU load values):

2025-03-03 [Link] CPU Load: 0.58

2025-03-03 [Link] CPU Load: 0.62

You might also like