Friday, February 5, 2016

Unix/Linux Bash Scripting Day 01

Braces expansion

$echo {1..10..2}  - produce 1 3 5 7 9
$echo {A..Z} - produce A B C ... X Y Z
$touch file{1..10} - produce file1, file2, file3,..,file10

Pipes and redirections

$ls  -l > /dev/null - redirect to nowhere, cannot be seen

Grep tool -  search contents of the file(s)

Awk tool

Sed tool

Network commands

$ping -c 1 www.theage.com.au - only display 1 line
$ping -c 1 www.theage.com.au | grep 'bytes from'
$ping -c 1 www.theage.com.au | grep 'bytes from' | cut -d = -f 4 - produce the turn around time, "-d =" indicates = is the delimiter, -f 4 indicate field number 4.

Bash shell scripts

 #!/bin/bash
# put comments here
ls -la

$chmod u+x myfirstcript.sh - will run myfirstshell without the bash command
$./myfirstscript.sh

Echo command

echo statement
echo 'statement'
echo "statement"

#!/bin/bash
#different uses of echo command

greetings="hello"
echo $greetings, world (planet)! # will produce errors due to no escape characters used
echo $greetings, world \(planet\)! # will produce the correct sentence.
echo '$greetings, world (planet)!' # will produce the whole string without using the variable $greetings
echo "$greetings, world (planet)!" # will use the variable $greetings





No comments:

Post a Comment

Mounting USB drives in Windows Subsystem for Linux

Windows Subsystem for Linux can use (mount): SD card USB drives CD drives (CDFS) Network drives UNC paths Local storage / drives Drives form...