Commands
cat
chmod
cp
mv
ls –l
ls
ls -a
date
export
find
grep
mkdir
rmdir
more
less
rm
sort
sleep
wc -l
kill
kill -l
diff
cd
sort
whence
umask
umask -S
who
bg
fg
ln
ln -s
tail
tail -f
head
what
who am i
history
exec
nice
arch
chown
mount
umount
Arithmetic expressions
<= - less than or equal to
>= - greater than or equal to
< - less than
> - greater than
== - equal to
!= - not equal to
* - multiplication
/ - division
+ - addition
- - subtraction
Shell variables used by ksh
PATH - Path search directories
SHELL - Pathname of the Shell
TERM - Terminal type
HOME - Your home directory
ENV - User environment file
Shell variables set by ksh
PWD - Working directory
LINENO - Current line number
PPID - Parent process id
Shell statements
printf “string” - output a string of information
echo $COUNTER - output content of the variable COUNTER
echo “Text line” - output text line to monitor from script
printf $NUMBER - output content of the variable NUMBER
NUMBER=$(($NUMBER-1)) - decrease NUMBER value by one
$# - number of positional parameters
$? - return value
$$ - process id of this shell
$! - background process id
$1 - first command line argument
$2 - second command line argument
Conditional Expression Primitives
-r - true if file exists and is readable
-w - true if file exists and is writable
-x - true if file exists and is executable
-f - true if file exists and is a file
-d - true if file exists and is a directory
-eq - true if value of exp1 and exp2 are equal
-ne - true if value of exp1 and exp2 are not equal
-gt - true if value of exp1 is greater than value of exp2
-ge - true if value of exp1 is greater than or equal to value of exp2
-lt - true if value of exp1 is less than value of exp2
-le - true if value of exp1 less than or equal to value of exp2
Quoting
‘ …’ - single literal quotes
“…” - double quotes (interpolation)
`…` - single back ticks (command execution)
I/O Operators
< Variable - read from file (cat < test_file)
> Variable - write to a file (cat > test_file)
Variable >> Variable - append content of file_1 to the end of file_2 (cat file_1 >> file_2)
& - Execute process in background
Syntax
while [ … ]
do
…
done
if [ … ]
then
…
fi
if [ … ]
then
…
elif
…
fi
test –r “$test_file”
test x+1 –gt y
AWK
The character "\" is used to "escape" or mark special characters. The list of these characters is in table below:
+-------------------------------------------------------+
| AWK Table 5 |
| Escape Sequences |
|Sequence Description |
+-------------------------------------------------------+
|\a ASCII bell (NAWK only) |
|\b Backspace |
|\f Formfeed |
|\n Newline |
|\r Carriage Return |
|\t Horizontal tab |
|\v Vertical tab (NAWK only) |
|\ddd Character (1 to 3 octal digits) (NAWK only) |
|\xdd Character (hexadecimal) (NAWK only) |
| Any character c |
+-------------------------------------------------------+
Let us create employee.txt file which has the following content, which will be used in the
examples mentioned below.
$cat employee.txt
100 Thomas Manager Sales $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA Technology $6,000
Awk has number of built in variables. For each record i.e line, it splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4. $0 represents whole line. NF is a built in variable which represents total number of fields in a record.
$ awk '{print $2,$5;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000
$ awk '{print $2,$NF;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000
In the above example $2 and $5 represents Name and Salary respectively. We can get the Salary using $NF also, where $NF represents last field. In the print statement ‘,’ is a concatenator.
Share with your friends: |