Shell Scripts Flashcards
What is a shebang?
!
Is a shebang mandatory for a script?
No, if no shebang is found the user configured shell will be used.
What permissions we need to set to be able to run a shell script?
755, u+x, +x
Can you define variables in a shell script?
Yes, VARIABLE_NAME=”Value”
Is this a correct way to define a variable variable_name=”value”
yes
is this a correct way to define a variable VARIABLE_NAME= “Value”
No, there should no be spaces before or after the equal sign.
What’s the syntax to reference a variable inside a shell script?
Two ways: $VARIABLE or ${VARIABLE}
How can we store the output of a command in a variable?
VARIABLE=$(ls), you can also use backticks VARIABLE=ls
, backticks is an older syntax, prefer dollar sign.
Is this a valid variable name 1FIRST=”Value”?
No, variables can’t start with numbers
What command should we use for test things inside of a shell script?
test
what’s the syntax of a test command inside of a shell script?
[ condition-to-test ] notice the spaces between the brackets, those are important, otherwise it won’t work.
Can you compare strings with test?
Yes, [ STRING1 = STRING2 ]
Can we use if statements in shell scripts?
Yes, in combination with test
Apart from if, what other syntax can you use for if cases in shell scripts?
if, then, else, elif, fi
Is there a way to loop in a shell script?
Yes, with for/do/done
What does $0 in a shell script means?
That’s the first parameter which is the name of the script
How can we get the third parameter of a script
$3
How can we reference all the parameters that are passed to a shell script
$@
What command should we use in shell script to let user enter some values from stdin and grab them in script?
read -p “Enter some value” VARIABLE
How can we write comments in a shell scripts?
any line that starts with # is a comment in a shell script and it’s ignore by the interpreter