Shell
General 43 questions
- #1What does this line in shell scripts means?: #!/bin/bash
- #2True or False? When a certain command/line fails in a shell script, the shell script, by default, will exit and stop running
- #3What do you tend to include in every script you write?
- #4Today we have tools and technologies like Ansible, Puppet, Chef, ... Why would someone still use shell scripting?
- #5How to define a variable with the value "Hello World"?
- #6How to define a variable with the value of the current date?
- #7How to print the first argument passed to a script?
- #8Write a script to print "yay" unless an argument was passed and then print that argument
- #9What would be the output of the following script?
#!/usr/bin/env bash NINJA_TURTLE=Donatello function the_best_ninja_turtle { local NINJA_TURTLE=Michelangelo echo $NINJA_TURTLE } NINJA_TURTLE=Raphael the_best_ninja_turtle - #10Explain what would be the result of each command:
- echo $0
- echo $?
- echo $$
- echo $#
- echo $0
- #11What is $@?
- #12What is difference between $@ and $*?
- #13How do you get input from the user in shell scripts?
- #14How to compare variables length?
- #15Explain conditionals and demonstrate how to use them
- #16In shell scripting, how to negate a conditional?
- #17In shell scripting, how to check if a given argument is a number?
- #18How to perform arithmetic operations on numbers?
- #19How to perform arithmetic operations on numbers?
- #20How to check if a given number has 4 as a factor?
- #21What is a loop? What types of loops are you familiar with?
- #22Demonstrate how to use loops
- #23How do you debug shell scripts?
- #24Running the following bash script, we don't get 2 as a result, why?
x = 2 echo $x - #25How to extract everything after the last dot in a string?
- #26How to extract everything before the last dot in a string?
- #27Generate 8 digit random number
- #28Can you give an example to some Bash best practices?
- #29What is the ternary operator? How do you use it in bash?
- #30What does the following code do and when would you use it?
diff
- #31What are you using for testing shell scripts?
- #32Argument Check
Objectives
Note: assume the script is executed with an argument
- Write a script that will check if a given argument is the string "pizza"
- If it's the string "pizza" print "with pineapple?"
- If it's not the string "pizza" print "I want pizza!"
- #33Basic Date
Objectives
- Write a script that will put the current date in a file called "the_date.txt"
- #34Count Chars
Objectives
- Read input from the user until you get empty string
- For each of the lines you read, count the number of characters and print it
Constraints
- You must use a while loop
- Assume at least three lines of input
- #35Directories Comparison
Objectives
- You are given two directories as arguments and the output should be any difference between the two directories
- #36Empty Files
Objectives
- Write a script to remove all the empty files in a given directory (including nested directories)
- #37Shell Scripting - Factors
Objectives
Write a script that when given a number, will:
- Check if the number has 2 as factor, if yes it will print "one factor"
- Check if the number has 3 as factor, if yes it will print "one factor...actually two!"
- If none of them (2 and 3) is a factor, print the number itself
- #38Files Size
Objectives
- Print the name and size of every file and directory in current path
Note: use at least one for loop!
- #39Great Day
Objectives
- Write a script that will print "Today is a great day!" unless it's given a day name and then it should print "Today is
"
Note: no need to check whether the given argument is actually a valid day
- Write a script that will print "Today is a great day!" unless it's given a day name and then it should print "Today is
- #40Shell Scripting - Hello World
Objectives
- Define a variable with the string 'Hello World'
- Print the value of the variable you've defined and redirect the output to the file "amazing_output.txt"
- #41It's Alive!
Objectives
- Write a script to determine whether a given host is down or up
- #42Number of Arguments
Objectives
- Write a script that will print "Got it:
" in case of one argument - In case no arguments were provided, it will print "Usage: ./
" - In case of more than one argument, print "hey hey...too many!"
- Write a script that will print "Got it:
- #43Sum
Objectives
- Write a script that gets two numbers and prints their sum
- Make sure the input is valid (= you got two numbers from the user)
- Test the script by running and passing it two numbers as arguments
Constraints
- Use functions