Scripting Advanced Flashcards
(14 cards)
How many return/exit codes are there?
How do you check the return/exit codes for grep?
0 - 255
info grep
man grep
Show the exit/return code for your previous command
$?
Create a script that pings google.com once and gives different messages depending on success.
Make it to where the ping doesn’t show up as stdout
You should exit the code and return a 1 if the host is not reachable and return a 0 if it is
google.com should be the value of a variable named HOST
What would you put instead if you wanted to print something if the ping command didn’t succeed?
HOST=’google.com’
ping -c 1 $HOST > /dev/null
if [ $? -eq “0” ]
then
echo “$HOST is reachable”
exit 1
else
echo “$HOST is not reachable”
exit 0
fi
if [ $? -ne “0” ]
What is the command for and
What is the command for or
&& - and
|| - or
Perform a command that makes a directory /tmp/bak/, if that returns a return code of 0, copy a file called text.txt to it
mkdir /tmp/bak && cp text.txt /tmp/bak/
Perform a command that makes a directory /tmp/bak/, if that returns a return code of 1, copy a file called text.txt to /home/delsinm
mkdir /tmp/bak || cp text.txt /tmp/bak/
If the first command doesn’t work it will go to the second and so on and so forth
And and Or will check the return code to decide if the next command should be ran. What option would make a command run regardless?
;
How do you specify your own exit/return code in a script?
What happens when you call this command?
What happens if you don’t have an exit code?
What happens if you have an exit code without a number following it?
exit 0
If you call this command and it is ran it will close out your script
The return status of the previously executed code is shown.
For the ping script we made it would always give a 0, because it would give a 0 for the echo command running successfully
What are the two ways to call a function?
function function-name() {
# code goes here
}
function-name() {
# code goes here
}
Commands are read in order but you functions will be read prior to execution, so you can call one in the first function that’s defined later as long as you don’t call the first function prior to the second function showing up.
Create two functions:
One the says hello
References to run the second function
Second function that shows the current time.
function hello() {
echo “Hello!”
now
}
function now() {
echo “It’s $(date+%r)”
}
hello
Using parameters, Create a function that displays your name. Give multiple names and loop through them
function hello() {
for NAME in $@
do
echo “Hello $NAME”
done
}
hello Jason Dan Scott
When can you use a global variable assigned in a function?
Show an example
my_function() {
GLOBAL_VAR=1
}
my_function
echo $GLOBAL_VAR
How do you create a local variable in a function?
my_function() {
local GLOBAL_VAR=1
}
my_function
echo $GLOBAL_VAR
How would we give and exit/return code for a function. This should stop the function, not stop the entire script.
How would you access that exit/return code?
!/bin/bash
my_function() {
echo “Inside function”
return 1 # Stops the function and sets the return code
echo “This won’t run”
}
my_function
echo $?
The $? should be applied directly after calling the function