Advanced Scripting section 2 Flashcards
(19 cards)
For the below directory, how would you get just it’s name for a script:
/etc/ansible/this.yml
basename /etc/ansible/this.yml
What do you use to get the running process of a script
$$
What are two methods you can you can use to run a command in a script?
`` - BackTicks
$()
This looks similar to ${} but that is for variables
What are the names for *
wildcard
glob
glob patters
What would you use to see what files start with a and have 0 or more characters between it followed by .txt?
What would you use to look for patterns that match a followed by only one character more followed by .txt
a*.txt
a?.txt
You are searching for the below files:
cat can
in a list with multiple words that start with c.
How would you only search for these two?
How would you avoid searching for these two letters
ca[tn]
ca[!tn]
search for characters a-n and 1-3
[a-n]
[1-3]
What are the predefined named character classes?
name them and use one to find a word that starts with an uppercase letter
[:alpha:]
[:alnum:]
[:digit:]
[:lower:]
[:space:]
[:upper:]
[[:alpha:]]
How would you find a file that ends with a wildcard, like a question mark?
backslash
*\?
backslash escapes the next character
What are the components of a CASE statement?
case $VAR in
pattern1)
command
command
;;
pattern2)
command
;;
*)
exit 1
;;
esac
Make a case statement that takes either start or START to run one command and stop or STOP from your first parameter to run another
case $1in
start|START)
/usr/bin/sshd
;;
stop|STOP)
KILL $(cat /var/run/sshd.pid)
;;
*)
exit 1
esac
Create a script that prompts you to enter y or n and stores it in a variable.
The variable can actually contain any of the following for one option:
y, Y, yes, YES, YEs, etc, Pretty much any way a moron can type it.
Do the same for no
What are you using to do this
read -p ‘enter y or n’ ANSWER
case $ANSWER in
[yY]|[yY][eE][nN])
echo “You answered yes”
;;
[nN]|[nN][oO])
echo “You answered no”
;;
*)
echo “Invalid answer”
;;
esac
CHARACTER CLASSES
How would you insert an echo multiline? What is this known as?
Put it in a variable
ANSWER=$(cat «something
This is line one
This is line two with more indentation
This is line three
something
)
you don’t have to put something, you could do EOF or anything you like as long as they match.
a HEREDOC
Name the syslog facilities including facilities for custome logs
category or source of the log message
man logger
local0-local7 - custom logs
0 kern - kernel message
1 user
2 mail
3 daemon
4 auth - security/authorization messages
5 syslog - messages generated by syslogd
6 lpr - line printer subsystem
7 news - network news subsystem
8 uucp
9 clock
10 authpriv security/authorization messages
11 ftp
15 cron
16-23 local0-7
In terms of syslog, what are the 8 severities
0-emergency
1-alert
2-critical
3-error
4-warning
5-notice
6-info
7-debug
Where are syslog messages generally stored?
What is the system logger for RedHat 9?
What command do you use to generate syslog messages?
What facility and severity does it use by default?
/var/log/messages
redhat uses rsyslogd as it’s system logger
logger
user/notice
Add a syslog message of “holy cow” to local0 facility and info as the severity
logger -p local0.info ‘holy cow’
What’s a good option to add to your logs and why?
How could you add the PID?
-t (name of the script)
This makes it easier to search through the logs to find your script error
-i adds the PID
logger -i -t script.sh -p local0.info ‘message’
output would look like this:
jan 23 04:11:52 localhost script.sh[1234] message
What option would you give if you wanted to send you log message to the stdout as well
-s
logger -si -t script.sh -p local0.info ‘message’