Shell Scripts and Grep Command
Shell Scripts and Grep Command
Shell Scripts and Grep Command
#!/bin/bash
# not a comment
[user@host ~]$ echo \# not a comment \#
# not a comment #
# not a comment #
The echo command is widely used in shell scripts to display informational or error messages
#!/bin/bash
Hello, world
vim firstscript.sh
#!/bin/bash
sh firstscript.sh
#####################################################
16. Make the firstscript.sh file executable using the chmod command.
#####################################################
COMMAND VARIABLE
Done
[user@host ~]$ for HOST in host1 host2 host3; do echo $HOST; done
echo "$EVEN";
done
2 4 6 8 10
The exit command can be executed with an optional integer argument between 0 and 255,
which represents an exit code. An exit code is a code that is returned after a process has
completed. An exit code value of 0 represents no error. All other nonzero values indicate an
error exit code. You can use different nonzero values to differentiate between different types
of errors encountered. This exit code is passed back to the parent process, which stores it in
the ? variable and can be accessed with $? as demonstrated in the following examples.
This exit code is passed back to the parent process, which stores it in the ? variable and can
be accessed with $? as demonstrated in the following examples.
#!/bin/bash
exit 0
Hello, world
The following examples demonstrate the use of the test command using Bash's numeric
comparison operators.
[user@host ~]$ test 1 -gt 0 ; echo $?
0
[user@host ~]$ test 0 -gt 1 ; echo $?
1
Conditional Structures
Using the if/then Construct
if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
Fi
The following code section demonstrates the use of an if/then construct to start the psacct
service if it is not active.
if <CONDITION>; then
<STATEMENT>
...
<STATEMENT>
else
<STATEMENT>
...
<STATEMENT>
Fi
Use the ssh and hostname commands to print the host name of servera and serverb to
standard output.
2. Verify that the newly created directory is in your PATH environmental variable.
3. [student@workstation ~]$ echo $PATH
4. /
home/student/.local/bin:/home/student/bin::/usr/local/bin:/usr/bin:/usr/loc
al/sbin:/usr/sbin
Option Description
. The period (.) matches any single character.
? The preceding item is optional and will be matched at most once.
* The preceding item will be matched zero or more times.
+ The preceding item will be matched one or more times.
Option Description
{n} The preceding item is matched exactly n times.
{n,} The preceding item is matched n or more times.
{,m} The preceding item is matched at most m times.
{n,m} The preceding item is matched at least n times, but not more than m times.
Alphanumeric characters: '[:alpha:]' and '[:digit:]'; in the 'C' locale and ASCII
[:alnum:]
character encoding, this is the same as '[0-9A-Za-z]'.
Alphabetic characters: '[:lower:]' and '[:upper:]'; in the 'C' locale and ASCII
[:alpha:]
character encoding, this is the same as '[A-Za-z]'.
[:blank:] Blank characters: space and tab.
Control characters. In ASCII, these characters have octal codes 000 through 037,
[:cntrl:] and 177 (DEL). In other character sets, these are the equivalent characters, if
any.
[:digit:] Digits: 0 1 2 3 4 5 6 7 8 9.
[:graph:] Graphical characters: '[:alnum:]' and '[:punct:]'.
Lower-case letters; in the 'C' locale and ASCII character encoding, this is a b c d
[:lower:]
e f g h i j k l m n o p q r s t u v w x y z.
[:print:] Printable characters: '[:alnum:]', '[:punct:]', and space.
Punctuation characters; in the 'C' locale and ASCII character encoding, this is! "
[:punct:] # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ' { | } ~. In other character sets, these
are the equivalent characters, if any.
Space characters: in the 'C' locale, this is tab, newline, vertical tab, form
[:space:]
feed,carriage return, and space.
Upper-case letters: in the 'C' locale and ASCII character encoding, this is A B C
[:upper:]
D E F G H I J K L M N O P Q R S T U V W X Y Z.
[:xdigit:] Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f.
Option Function
Use the regular expression provided but do not enforce case sensitivity (run
-i
case-insensitive).
-v Only display lines that do not contain matches to the regular expression.
Apply the search for data matching the regular expression recursively to a group
-r
of files or directories.
-A
NUMBER
Display NUMBER of lines after the regular expression match.
-B
NUMBER
Display NUMBER of lines before the regular expression match.
With multiple -e options used, multiple regular expressions can be supplied and
-e
will be used with a logical OR.
The following example prints the matched line, along with the 3 lines after it.
$ grep -B 2 "single WORD" demo_text (matched line along with 3 lines before)
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
^ : matching the lines that start with a string grep “^unix” tes.txt
$ : matching the lines that end with a string grep “os$” tes.txt
In cases where you know what you are not looking for, the -v option is very useful. The -v
option only displays lines that do not match the regular expression. In the following example,
all lines, regardless of case, that do not contain the regular expression server are returned.
To look at a file without being distracted by comment lines use the -v option. In the following
example, the regular expression matches all lines that begin with a # or ; (typical characters
that indicate the line will be interpreted as a comment). Those lines are then omitted from the
output.
The grep command with the -e option allows you to search for more than one regular
expression at a time. The following example, using a combination of less and grep, locates
all occurrences of pam_unix, user root and Accepted publickey in the
/var/log/secure log file.
To search for text in a file opened using vim or less, use the slash character (/) and type the
pattern to find. Press Enter to start the search. Press N to find the next match
/Daemons
[root@host ~]# less /var/log/messages
/device
The postfix package was installed today by the start script. Use the grep command to find
the GID and UID for the postfix and postdrop groups and users. To reduce the output of
the grep command, display all logs from a specific Start Time.
2. Use the grep command with the date, start time, and GID options to find the postfix
and postdrop user's GID and UID. The lab set-up script ran a few minutes before the
current time. Take this into consideration when searching the /var/log/secure log
file.
Modify your regular expression to locate the first two messages in the /var/log/maillog
file. Notice that in this search you are not using the caret character (^) because you are not
searching for the first character in a line.
You are required to find the name of the queue directory for the Postfix server. Search the
/etc/postfix/main.cf configuration file for all information about queues. Use the -i option
to ignore case distinctions.
/Postfix
Use the ps aux command to confirm that the postfix server is currently running. Reduce the
output of ps aux by combining it with the grep command.
Confirm that the qmgr, cleanup, and pickup queues are correctly configured. Use the grep
command with the -e option to match multiple entries in the same file. The configuration file
is /etc/postfix/master.cf
Edit your newly created script file to comply with the following requested information from
the servera and serverb hosts. The systems are configured to use SSH keys for
authentication; a password is not required.
USR='student'
OUT='/home/student/output'
do
done