0% found this document useful (0 votes)
4 views

Shell Practice

The document provides a comprehensive overview of shell scripting, including definitions of key concepts such as shell, Bash, and shell scripts. It covers how to write, run, and manage shell scripts, as well as various programming constructs like variables, functions, loops, and conditionals. Additionally, it addresses file and directory operations, process management, text processing, networking, system information, package management, and user management commands.

Uploaded by

LAKKI
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)
4 views

Shell Practice

The document provides a comprehensive overview of shell scripting, including definitions of key concepts such as shell, Bash, and shell scripts. It covers how to write, run, and manage shell scripts, as well as various programming constructs like variables, functions, loops, and conditionals. Additionally, it addresses file and directory operations, process management, text processing, networking, system information, package management, and user management commands.

Uploaded by

LAKKI
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/ 13

1. What is a shell?

A: Shell is an intermediatory between user and Operating system—it provides user with an
interface.

2.What is Bash?

A: Bash is one kind of shell in Unix –commonly default shell

3.What is a shell script?

A: Group of Unix commands

4. How do you write a shell script?

 Using Vi or Vim(file editors) file name should have .sh extenstion.


 Start with Shebang #! /bin/bash

5.What is the file extension for a shell script?

A: .sh

6. How do you run a shell script?

A:Sh filename or ./filename

7. What is the shebang line?

A: Shebang is the first command line in the script

8. What is the purpose of the shebang line?

A: Shebang is the command line to run the script in specific shell

9. What is a comment in a shell script?

A: Comment is the Text that details about the steps in script.

10. How do you write a comment in a shell script?

A: # (single line) or “”(Multi line)

11. What is a variable in a shell script?

A: Variable is a placeholder of the value

12. How do you create a variable in a shell script?

A: Using $

13. What is the syntax for a variable in a shell script?

A: variable name = variable value

14. What is the scope of a variable in a shell script?

A: Local and Global variables

15. What is command substitution?


A: Output of one command as input for another command

16. How do you use command substitution in a shell script?

A: $()

17. What is command chaining?

A: Different commands merged together using different operators.

18. How do you use command chaining in a shell script?

A: By using different operators like &, && ,|| ,: ,| ,>>,>

19. What is a function in a shell script?

A: Function are set of commands which will execute when called/initiated.

20. How do you create a function in a shell script?

A: function_name() { Unix commands }

21. What is the syntax for a function in a shell script?

A: function_name() { Unix commands }

22. What is the return value of a function in a shell script?

A: Value (Output value)

23. How do you call a function in a shell script?

A: Functionname (By calling the function name)

24. What is a conditional statement in a shell script?

A: Command that executes based on the condition provided

25. How do you use a conditional statement in a shell script?

A: if [ expression ]

then

statement

fi

26. What is a loop in a shell script?

A: repetitive execution of statement until condition is met

27. How do you use a loop in a shell script?

A: using for loop, while loop, untill loop


28. What is an array in a shell script?

A: multiple values grouped together

29. How do you create an array in a shell script?

A: Declare -a arrayname or

arrayname =(value1 value2 .... value…n)

30. What is the syntax for an array in a shell script?

A: arrayname =(value1 value2 .... value…n)

31. How do you access elements in an array in a shell script?

A: Arrayname[indexplace]

32. What is input/output redirection?

A: Process of directing input/output of one command/file to another file/command

33. How do you use input/output redirection in a shell script?

o >> append to the end of file


o | pass the left output to right
o > Redirect left output to right file
o Grep command to search

34. What is a pipeline in a shell script?

A: Pipeline is sequence of commands commonly output of left is transferred to the right of


the pipe command.

35. How do you use a pipeline in a shell script?

A: Command | command

36. What is a command-line argument?

A: Run time values for commands

38. What is the $0 variable in a shell script?

A: Name of the script file name

39. What is the $1 variable in a shell script?

A: , $1 denotes the initial argument passed,


40. What is the $2 variable in a shell script?

A: second argument passed

41. What is the $# variable in a shell script?

A: no of arguments passed

42. What is the $@ variable in a shell script?

A: List of arguments

43. What is the $? variable in a shell script?

A: Exit status code

44. What is the $$ variable in a shell script?

A: Returns the process ID of the shell

45. What is the $! variable in a shell script?

No idea on signals I will read on this

46. What is a signal in a shell script?

47. How do you handle signals in a shell script?

48. What is process substitution?

A: Comparing two different outputs from different commands.


50. What is a here document?I

A: It is a form of I/O redirection

***Need to look into these*******

50. What is a here document?

51. How do you use a here document in a shell script?

52. What is a here string?

53. How do you use a here string in a shell script?

******************************************

54. What is a glob?

A: A glob is a string of characters that matches file paths, using literal characters

55. How do you use a glob in a shell script?

56. What is brace expansion?

A: a useful technique to generate lists of strings that can be used in scripts and aliases and on the
Linux command line.

57. How do you use brace expansion in a shell script?

A: echo {x…y ..z} generates values from x to y incrementing by z.

58. What is a subshell?

A: subshell is a copy of the shell process.

59. How do you create a subshell in a shell script?

A: By enclosing the variables or commands in parenthesis

60. What is a background process?

A: process that is running independent of the shell.

61. How do you run a process in the background in a shell script?

A: using “nohup” command.

62. What is a foreground process?

A: a process that requires a user to start


63. How do you run a process in the foreground in a shell script?

A: Using fg command

64. What is the difference between a foreground process and a background process?

A: Process that requires user interaction is foreground process—process which is


independent for user interaction is called backend process.

65. What is a job in a shell script?

A: "job" is a shell's representation for a process group.

66. How do you manage jobs in a shell script?

67. What is a signal trap?

A: The trap command allows you to control the way a program behaves when it receives a
signal.

68. How do you use a signal trap in a shell script?

A: trap followed by command to be executed

69. What is a sourced file?

A: source is a Bash shell built-in command that executes the content of the file passed as an
argument in the current shell.

70. How do you source a file in a shell script?

A: source <fileName> or. ./<filename>

71. What is a script library?

A: sets of shell functions that you can import

72. How do you create a script library in a shell script?

73. What is a script module?

A: is a fragment of code that can be dynamically loaded into the Linux kernel without
requiring a reboot

74. How do you create a script module in a shell script?

A: Module “module_name”

75. What is a script package?

A: are simple containers that contain all the required artifacts necessary to run a script.

76. How do you create a script package in a shell script?

77. What is a script template?

A: a reusable packaged script that you can use when creating a new script

78. How do you create a script template in a shell script?


79. What is a script generator?

A: simplifies script creation for Unix and Linux systems.

80. How do you create a script generator in a shell script?

A:

81. What is a script runner?

82. How do you create a script runner in a shell script?

83. What is a script debugger?

A:It is a built-in feature that allows you to see what happens when your script is executed

84. How do you create a script debugger in a shell script?

A: Set -x

85. What is a script profiler?

86. How do you create a script profiler in a shell script?

87. What is a script unit tester?

A: Unit tests focus on one part of an application in total isolation. Usually, that means a
single class or function.

88. How do you create a script unit tester in a shell script?

89. What is a script integration tester?

A: helps find issues that are not obvious by examining the implementation of an entire
application or one specific unit,

90. How do you create a script integration tester in a shell script?

91. What is a script acceptance tester?

92. How do you create a script acceptance tester in a shell script?

93. What is a script deployment tool?

A: you call make (or Ant or whatever) to build the package and then call whatever
deployment tool you use for deployment

94. How do you create a script deployment tool in a shell script?

95. What is a script version control system?

A: Software tools that help software teams manage changes to source code over time. A

96. How do you use a script version control system in a shell script?

A: SVN or Git

97. What is a script documentation tool?


98. How do you create a script documentation tool in a shell script?

99. What is a script build system?

A: help compile your code in a neat and structured way,

File and Directory Operations:

1. List all files and directories in the current directory.


A: ls -a

2. Create a new directory named "my_folder."


A: mkdir my_folder
3. Move a file named "file.txt" from the current directory to "my_folder."
A: mv file.txt /path/my_folder
4. Display the contents of a file named "sample.txt."
A: cat sample.txt

5. Delete the directory "my_folder" and its contents.


A: rm -r my_folder

Process Management:

6. List all running processes.


A: ps -a

7. Find the process ID of a process named "nginx."

A: pidpof process nginx

8. Kill a process with a specific process ID.

A: kill -9 PID

9. Start a command in the background and then bring it to the foreground

10. Run a command and make it immune to hangups using nohup .

A: nohup [command] - run <command>


Text Processing:

11. Search for the word "error" in a file named "log.txt."


A: grep -i error log.txt

12.Extract the first column from a CSV file.


A: awk '{print $1}' my_file.txt

13. Count the number of lines in a file.


A: wc -l filename

14. Replace all occurrences of "old_text" with "new_text" in a file.


A: sed -i 's/old-text/new-text/g' filename

15. Display the last 10 lines of a file named "output.log."


A: tail -10 filename

Networking:
16. Ping a specific IP address to check its connectivity.
A: ping IPAddress

17. Display network information for all network interfaces.


A: netstat -in

18. Download a file from a specific URL using wget .


A: wget httpurl

19. Transfer a file named "data.txt" to a remote server using scp .

A: scp [options] [[user@]host1:]data.txt ... [[user@]host2:]destination

20. Establish an SSH connection to a remote server.


A: SSH-keygen -copy the pub key to auth_key file in other server.

System Information:

21. Display the amount of free and used disk space.


A: df -a
22. Check the available memory on the system.
A:top/ free -m

23. Display the system's uptime.


A: uptime

24. List all currently logged-in users.


A; Who

25. Find out the operating system type using uname .

A: uname -a

Package Management:

26. Install a package named "nginx" using the appropriate package manager.
A: yum install nginx -y

27. Update all installed packages on the system.

A: yum update all

28. Search for a package named "python" using the package manager.
A: yum list all | grep -I python

29. Remove a package named "apache" using the package manager.

A : Yum remove packagename

File Search:

30. Locate all files with the extension ".txt" on the system.

A: find . -type f -name "*.txt"

31. Find the location of the executable file for the command "ls."
A: whereis ls

32. Search for files relate to a specific program using whereis .

A: whereis filename
File Editors:
33. Open a file named "notes.txt" for editing using nano .
A: nano filename

34. Edit a file named "config.conf" using the vi or vim editor.

A: vim config.conf

35. Save and exit the editor after making changes to a file.
A: :wq! And enter

System Control:

36. Reboot the system using the appropriate command.


A: shutdown -r now

37. Shut down the system gracefully.


A: shutdown -h now

38. Halt the system immediately.

A: halt -l.

User Management:

39. Create a new user account named "john."


A: useradd john

40. Modify the password of the user account "mary."


A: passwd mary
41. Delete a user account named "jane."
A: userdel username

Compression/Decompression Utilities:

42. Compress a file named "data.txt" using


gzip .
A: gzip -r data.txt

43. Decompress a file named "archive.gz" using gunzip .


A: gunzip archive.gz

44. Create a tar archive named "backup.tar" containing the files in the directory
A: tar -c backup.tar /filepath/directoryname
6. How do you run a shell script?

You might also like