Proposal Student Competition Enhancement

Download Report

Transcript Proposal Student Competition Enhancement

Module 6 –
Redirections,
Pipes and Power
Tools.
Redirections
•STDin 0
•STDout 1
•STDerr 2
Redirections
• Capture STDout with 1> or simply >
• Append STDout with >>
# ls > list.txt
Redirections
•Capture STDerr with 2>
•Append STDerr with 2>>
# ls bogos.txt 2> errors.txt
Merging Standard Output With
Standard Error
• Merge standard output and
standard error in to the same file
with 2>&1 – binding the streams
together
# ls ort.txt bogos.txt 2>&1 both.txt
Regular Expressions (RegEx)
.
Matches any single character
[list]
Matches any single character in the list
[range]
Matches any single character in range
[^ ]
*
Matches any single character not in list or range
Matches previous character 0 or more times
\{n\}
Matches previous character n times
\{n,\}
Matches previous character at least n times
\{n,m\}
Matches previous character between n and m times
^
Matches regular expressions at the start of the line only
$
Matches regular expressions at the end of the line only
\
Quote recognition of special characters
Pipes
• A pipe ( | ) takes the output of the
command and passes it as an input into
the following command.
• A pipe will continue to pass STDout
even if there is an error. he will simply
drop STDerr
# ls -l /etc | wc -l
The grep command
• The grep command is used to search text or searches
the given file for lines containing a match to the given
strings or words. By default, grep displays the
matching lines.
# grep ro /etc/passwd
Or
# cat /etc/passwd | grep ro
-i, --ignore-case
# cat /etc/httpd/conf/httpd.conf | grep -i server
Combining grep with regex
# ls -l /etc | grep "^d“
# ls -l /etc | grep "d$“
# grep '50[0-9]'
/etc/passwd
# ifconfig | grep -o '[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]\.[0-9][0-9][0-9]‘
# ifconfig | grep -o '[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}\.[0-9]\{3\}'
The cut command
• The cut command can cut fields.
• We need to define the delimiter and we need to tell
the cut command which field to cut:
# cat /etc/passwd | cut –d”:” -f1
!!! Don’t forget to sort the spaces for cut with the tr
command.
The cut command
• The cut command can cut fields.
# ls -l /tmp | cut –d” ” -f1,5
!!! Don’t forget to sort the spaces for cut with the tr
command.
The tr command
• The tr is used to squeeze multiple spaces into one
space, helping the cut command in cutting the rows:
# ls -l | tr -s “ “ | cut -d” “ –f1,4
!!! note
the tr command “need” a space after the –s option
The sort command
• By default the sort command sort files in alphabetical
order.
• We can sort by numbers with -n
#
ls -l /etc | sort
!!! The sort command usually comes before the uniq
command.
The uniq command
• The uniq command can identify uniq entries,
duplicate entries and can also count those entries.
# uniq
# uniq -d
# uniq -dc
(duplicates)
(duplicates and count)
awk
• Although awk is a complete pattern scanning and
processing language, it is most commonly used as a
Unix command-line filter to reformat the output of
other commands.
• Basic awk example:
# cat /etc/passwd | awk ‘{print $1}’
sed
• sed is a steam editor
• We used sed earlier when we learned Search&Replace in VI.
Remember VI ?
:1,$s/old/new/g
And in the shell:
#
#
sed –i 's/SELINUX=enforcing/SELINUX=disabled/g'
sed -n '/ort/p' passwd >
ort.txt
/etc/sysconfig/selinux
Unix/Linux Exit Status
• A process is considered to have completed correctly
in Linux if its exit status was 0.
• Any other result greater then 0 is considered an error.
To find the exit status:
# echo $?
Chaining Commands
•;
Run multiple commands in one run, sequentially.
# ls ; echo hello ; tail -2 /etc/passwd
• & Sends process background(run in background
paralleling )
# ./script.sh &
• && What the double-ampersands are telling the system is
"wait for the first command to successfully complete. If it
does complete successfully, run the next command".
Search Commands
• updatedb + locate
• which and whereis
• The find command (as root)
# find / -name passwd
Exercise
1. Use redirection to save the ls command to a file.
2. Generate an error with ls and save the errors in a
file
3. Generate both “good” STDout and “bad” STDerr
and save them both in one file.
4. Count the directories under /home
5. Count all the users in the system
6. print only the permissions and the file-name fields
using the cut command
7. Use awk to print the first and last field from
/etc/passwd
8. use the find command to search for all files and
directories with the name of passwd
Exercise
9. Log-in to the instructor server and find how many
users are logged-in more then once and how many
times are those users logged-in.
10. Where is the binary of the fdisk tool ?
11. find all the files and directories in the system with
full permissions (777)
12. Find all the files in the system bigger then 2MB
13. Use the df command to print only the directory
name (mount point) and the size
14. Use the fdisk tool to print only the disks without the
partitions.
15. Copy /etc/passwd to your home dir and replace the
shell from bash to sh
<Insert Picture Here>