Start of Shell Scripting for Beginners Quiz
1. What is a shell script?
- A shell script is a graphic interface for user interaction.
- A shell script is a program written for the shell of an operating system.
- A shell script is a command used to install software.
- A shell script is a type of database used for storage.
2. How do you execute a shell script?
- `bash scriptname.sh`
- `execute scriptname.sh`
- `run scriptname.sh`
- `chmod +x scriptname.sh && ./scriptname.sh`
3. What is the default shell in Linux?
- Bash
- Fish
- Zsh
- Tcsh
4. How do you create a variable in a shell script?
- value=VAR
- VAR: value
- assign VAR=value
- VAR=value
5. How do you read user input in a shell script?
- read input
- scan input
- fetch input
- input read
6. How do you create a function in a shell script?
- function_name() { commands }
- create function_name[] { commands }
- function_name: commands;
- function_name = { commands }
7. What are positional parameters in a shell script?
- Positional parameters are environment variables set within the script.
- Positional parameters are used to pass arguments to a script and are accessed using `$1`, `$2`, etc.
- Positional parameters define the global variables for the shell.
- Positional parameters specify the path of the script being executed.
8. How do you check if a file exists in a shell script?
- [ -f filename ]
- [ -e filename ]
- [ -s filename ]
- [ -d filename ]
9. How do you compare two numbers in a shell script?
- [ $a = $b ]
- [ $a -eq $b ]
- [ $a -equal $b ]
- [ $a == $b ]
10. What is the purpose of `#!/bin/bash` at the beginning of a script?
- This line indicates the script is written in Python programming language.
- This is a shebang line that tells the system which interpreter to use to execute the script.
- This line sets environment variables for the script execution.
- This line specifies the version of Bash to use with the script.
11. How do you make a script executable?
- Write `scriptname.sh` in a text editor.
- Make a copy of scriptname.sh.
- Use the `chmod +x scriptname.sh` command.
- Type `execute scriptname.sh` in the terminal.
12. How do you create an infinite loop in a shell script?
- for i in 1; do commands; done
- while 1; do commands; end
- while true; do commands; done
- repeat until true; do commands; done
13. How do you write comments in a shell script?
- Use the `#` symbol to write comments.
- Use the `See also Debugging Techniques in Browsers Quiz
- Use `end` to terminate the script`s execution.
- Use the `exit` command followed by a number, e.g., `exit 0`.
- Use the `stop` command to finish the script.
- Use the `finish` command followed by a message.
16. What are shell script’s special variables?
- `$0`, `$#`, `$@`, `$?`
- `$$`, `*$`, `%%`, `!@`
- `*()`, `*+`, `$%`, `!#`
- `@!`, `&*`, `#^`, `??`
17. How do you declare an array in a shell script?
- arr[] = (item1 item2 item3)
- array{element1, element2}
- array=(element1 element2 element3)
- array: [element1, element2]
18. How do you access array elements in a shell script?
- `array[index]`
- `${array[index]}`
- `array(index)`
- `array{index}`
19. What is the IFS variable?
- IFS is a variable for handling network connections in scripts.
- IFS determines the maximum length of variables in shell scripts.
- IFS stands for Internal Function Syntax and handles script errors.
- IFS (Internal Field Separator) defines the character used for word splitting.
20. What is the use of the `exec` command?
- `exec` opens a file for reading.
- `exec` creates a new shell session.
- `exec` is used to compile scripts.
- `exec` replaces the current shell with a specified command.
21. How do you use the `find` command in a shell script?
- Example: `search filename`.
- Example: `locate filename`.
- Example: `grep filename`.
- Example: `find /path -name filename`.
22. How do you pass arguments to a shell script?
- Arguments can only be hardcoded inside the script.
- Arguments need to be declared in the script beforehand.
- Use flags like `-a`, `-b`, `-c` to pass arguments.
- Arguments are passed as positional parameters, accessed using `$1`, `$2`, etc.
23. What is a here document?
- A here document is a file containing only binary data.
- A here document allows you to redirect multiple lines of input into a command, using the `<<` delimiter.
- A here document is a type of script error handling technique.
- A here document is an interactive command line interface.
24. How do you handle errors in a shell script?
- Always print error messages without stopping the script.
- Use a try-catch block to manage exceptions.
- Check the exit status of commands and use conditional statements to handle errors.
- Ignore errors and continue executing the script.
25. What is the use of the `sleep` command?
- `sleep` deletes temporary files.
- `sleep` starts a new shell session.
- `sleep` pauses the execution for a specified amount of time.
- `sleep` compiles a program from source code.
26. How do you find the length of a string in a shell script?
- Use `${#string}`.
- Use `size(string)`.
- Use `length(string)`.
- Use `strlen(string)`.
27. How do you substring a string in a shell script?
- Use `substring(string, position, length)`.
- Use `${string:position:length}`.
- Use `string.substring(position, length)`.
- Use `slice(string, start, end)`.
28. How do you replace a substring in a string in a shell script?
- Use `string[search]=replace`.
- Use `${string//search/replace}`.
- Use `string.replace(search, replace)`.
- Use `replace(string)` method.
29. How do you check if a string is empty in a shell script?
- [ -z `$string` ]
- [ $string -lt `` ]
- [ `$string` == `` ]
- [ `$string` -eq 0 ]
30. How do you concatenate strings in a shell script?
- Use `string1.append(string2)`.
- Use `concat(string1, string2)`.
- Use `string1 + string2`.
- Use `string1=$string1$string2`.