Monday, January 31, 2011

BASH : Select Menus

Select - use the PS3 prompt
When you login into the bash enviroment you login using the PS1 promt by default
env | grep PS
set | grep PS -> RESULT
GROUPS=()
PS1='[\u@\h\W]\$ ' -> contains info regarding the username, host and working direcotry
PS2='> '
PS4='+ '
When you invoke 'select' from the bash script the PS3 will get the default value


nano select.sh
CONTENT OF FILE
#! /bin/bash
PS3='Please select a choice: '
select var in "Var1 Var2"
do
echo Hello World
command
break
done
#END
. select.sh ->result
1) Var1
Helo World


CONTENT OF FILE
#! /bin/bash
PS3='Please select a choice: '
LIST="Var 1 Var2"
select i in $LIST
do
if [ $i = "Var1"]
then
echo Hello World
elif [$i = "Var2"]
then
echo Goodbye World
fi
break
done
#END
CONTENT OF FILE
#! /bin/bash
PS3='Please select a choice: '
LIST="Var 1 Var2 Quit"
select i in $LIST
do
if [ $i = "Var1"]
then
echo Hello World
elif [$i = "Var2"]
then
echo Goodbye World
elif [ $i = "Quit"]
then
exit
fi
# break
done
#END
chmod u+x select.sh
./select.sh -> in this was selecting option 3-Quit we get out of the subshel. When we execute ./select.sh a subshell is created
CONTENT OF FILE
#! /bin/bash
PS3='Please select a choice: '
LIST="MySQL System Quit"
select i in $LIST
do
if [ $i = "MySQL"]
then
clear
watch tail /var/log/mysqld.log
elif [$i = "System"]
then
clear
watch tail /var/log/messages
elif [ $i = "Quit"]
then
exit
fi
# break
done
#END

BASH : Positional parameters

Positional parameters are separated by spaces
. forloop.sh 2 7
touch posparam.sh
CONTENT OF FILE
#!/bin/bash
#Author
#Date
#Purpose
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
#END
. posparam.sh 1 -> returns 1
. posparam.sh 3 -> returns 1
. posparam.sh 4 7 -> returns 2
. posparam.sh 4 78 -> returns 2


pico posparam.sh
CONTENT OF FILE
#!/bin/bash
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
seq $1 $2
#END
. posparam.sh 1 100 -> counts from 1 to 100 ; You have to give 2 parameters to the script
CONTENT OF FILE
#!/bin/bash
BADPARAM=165
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
if [ $# != 2] #you must have 2 argument given to this file
then
echo This script requires 2 arguments
exit $BADPARAM
fi
#Begin Sequence from param 1 to param 2
seq $1 $2
#END
. posparam.sh
chmod u+X posparam.sh
./posparam.sh -> close the shell window and he return code is 165 and message 'This script requires 2 arguments'
./posparam.sh 1 10 -> counts from 1 to 0
CONTENT OF FILE
#!/bin/bash
BADPARAM=165
echo $#  -> the '#' pound sign is the name of the variables and '$#' are the number of positionals parameters
if [ $# != 2] #you must have 2 argument given to this file
then
echo This script requires 2 arguments
echo "You entered $# parameters"
exit $BADPARAM
fi
#Begin Sequence from param 1 to param 2
seq $1 $2
#END
./posparam.sh -> close the shell window and he return code is 165 and message 'You  have entered 0 parameters'
echo ${10} -> access the 10th parameter from the script

BASH : Control Structures

touch file.sh
CONTENT OF FILE
#!/bin/bash
if [ 1 -eq 1]
then
echo Equal
else
echo Not Equal
fi
#END
CONTENT OF FILE
#!/bin/bash
if [ "text1" = "text1"]
then
echo Equal
else
echo Not Equal
fi
#END
CONTENT OF FILE
#!/bin/bash
for countries in USA Germany France
do
if [ "$countries" = "USA"]
then
echo Welcome to the $countries
elif [ "$countries" = "Germany"]
then
echo One country
else
echo $countries
fi
done
#END


. file.sh | sort
CONTENT OF FILE
#!/bin/bash
$FILE=helloworld.sh
if [ -e $FILE] #check if a file exists on computer
then
echo the file exists
else
echo the file does not exists
#END


#!/bin/bash
$FILE="test"
if [ -d $FILE] #check if a directory exists on computer
then
echo the directory exists
else
echo the directory does not exists
#END


touch file{1,2} -> create empty files
CONTENT OF FILE
#!/bin/bash
$FILE1="test1"
$FILE2="test2"
if [ $FILE1 -nt $FILE2] # FILE1 is newer than FILE2
then
echo FILE1 is newer than FILE2
else
echo FILE2 is newer than FILE1
#END
CONTENT OF FILE
#!/bin/bash
netstat -ant | greo :80
APACHESTATUS="$?"
if [ "$APACHESTATUS" -eq 0]
then
echo Apache is Up and Running
fi
#END


service httpd stop -> stops httpd service
netstat -ant | grep :80
service httpd start -> start httpd service
CONTENT OF FILE
#!/bin/bash
netstat -ant | greo :80 > /dev/null #this is a black holl
APACHESTATUS="$?"
if [ "$APACHESTATUS" -eq 0]
then
echo Apache is Up and Running
netstat -ant | grep 3306 #test mysql > /dev/null
MYSQLSTATUS="$?"
if [ "$MYSQLSTATUS" != 0]
then
echo MySQL is NOT running
else
echo MYSql is running
fi
else
echo Apache is not running
fi
#END


service mysqld stop


CASE sentence

#!/bin/bash
for countries in USA Germany France Armenia
do
case $countries in USA )
echo "Welcome to America" ;;
Germany )
echo "You are in GErmany" ;;
France )
echo "You are in France" ;;
* ) #default case
echo "Welocome to another country" ;;
esac #close case statemant
done
#END
. forloopcase.sh
wc -l . forloopcase.sh - showas the number of lines

BASH : While, Until Loops

While Loop - tests at the begining to see if the exit status is zero; Loop while true and when becomes false stop
CONTENT OF FILE
#!/bin/bash
NUM=0
MAX=10
while [ "$NUM" -lt "$MAX" ] #while [ "$NUM" -le "$MAX" ] #while [ "$NUM" -gt "$MAX" ]
do
echo $NUM
let "NUM += 1"
done
#END
Result -> count from 0 to 10
while ["``"] -> while loop search for a status. If the status is 0 continue to work else will finish


Until Loop - tests until the exit status is zero; Loop while false and when becomes true stop
touch untillloop.sh
CONTENT OF FILE
#!/bun/bash
NUM=100
MIN=20
#until [ condition-is-true]; do command done
until [ "$NUM" -eq "$MIN" ]
do
echo $NUM
let "NUM -= 1"
#END
Results -> count from 100 from 20
CONTENT OF FILE
#!/bun/bash
until [ "$STATUS" -eq "0" ]
do
ping -c 1 192.168.1.111 #bad host
echo The host is down
STATUS=`echo $?`
#END

BASH : For loop

touch forloop.sh
CONTENT of FILE
#!/bin/bash
#for arg in [list]; do action item done
for country in USA Uganda Germania
do
echo $country
done
#END


3 ways to run a file
1.  source ./forloop.sh -> print all 3 countrys
2. . forloop.sh
3. ./forloop.sh


. forloop.sh | sort -> sort in alfabetical order
. forloop.sh | sort > country.txt -> redirect the output into file
cp forloop.sh forloop2.sh
CONTENT of FILE
#!/bin/bash
for file in 'ls -A'
do
echo $file
done
#END
. forloop2.sh -> prints all the files from the current direcotry
CONTENT of FILE
#!/bin/bash
DIR="/etc"
for file in 'ls -A $DIR'
do
echo $file
done
#END
. forloop2.sh -> prints the file from /etc folder
. forloop2.sh | wc -l -> prints the numbers of files from /etc folder


cp forloop2.sh forloop3.sh
pico forloop3.sh
CONTENT of FILE
#!/bin/bash
for num in 'seq 1 100'
do
echo $num
done
#END
Results -> create numbers from 1 to 100
cp forloop3.sh forloop4.sh
pico forloop4.sh
CONTENT of FILE
#!/bin/bash
for user in 'cat /etc/passwd'
do
echo $user
done
#END
The results are unreadable


pico forloop4.sh
CONTENT of FILE
#!/bin/bash
DIR="/etc/passwd"
COUNT=0
for user in `cat $DIR | cut -f 1 -d ':'`
do
echo $user
let "COUNT += 1" # increment Count
done
echo There are $COUNT users registered
exit 0
#END
->returns the users from /etc/passwd
cat /etc/passwd | wc -l -> return the number of users

Get Iphone folders with WinSCP

1. Install WinSCP
2. Get the IP of your iphone :192.168.1.106
3. Connect with WinSCP
user:root
password:alpine
FOLDERS
/var/mobile/Media/Videos - movies made with Cycorder
/private/var/mobile/Media/iTunes_Control/Music/F00 to F13 - music
/private/var/mobile/Media/DCIM/100APPLE - pictures
/private/var/mobile/Documents - youtube movies downloaded with YouTube Downloader - aTube

BASH : Functions

Example:
touch functiontest.sh
pico functiontest.sh
#!/bin/bash
# Date:
# Author:
# Purpose:
# Created:
# Modified:
function showdate()
{
date +%F
}
function showtime()
{
date +%r
}
function getuserinfo()
{
clear
echo Please enter your firstname and last name
read firstname lastname
echo Hello $fisrtname $lastname
}
function mailadmin()
{
echo success | mail -s "Success Executions of script" root
}
showtime
showdate
getuserinfo
#mailadmin
# END
source ./functiontest.sh

BASH : First Script example Hello World

echo hello world -> prints 'hello word'
pico helloworld.sh
CONTENT of file
#!/bin/bash - shel interpreter
clear
echo hello world
#end
ls -ltr -> shows the last file created at the bottom
source helloword.sh -> prints 'hello world'
source executes a 'sh' file even if it has no permisions
./helloworld -> Permission denied
echo $? - > result 126
'./' - means this directory
'tab' completion works for executable files, if the file is not executable than the 'tab' won't work
chmod u+x helloworld.sh
./helloword.sh -> prints 'helloworld'
MODIFY FILE helloworld.sh
clear 
echo "hello world $" -> prints all
echo "hello world $value" -> prints 'hello world'
echo 'hello world $value' -> prints 'hello world $value'


MESSAGE="hello world"
clear
echo '$MESSAGE' -> prints '$MESSAGE'
echo "$MESSAGE" -> prints 'hello world'
echo $MESSAGE -> prints 'hello world'
#clear -> comment line
we can run ./helloworld.sh | wc -> returns the number of lines, words, characters
./helloworld.sh > text.txt -> overrides the content of text.txt
./helloworld.sh >> text.txt -> appends the content to text.txt


echo What is your name?
read name -> read something from standard input
clear
echo Hello $name -> prints 'Hello $name'
echo How are you doing?
read feelingecho You said you were feeling $feeling


echo Script name: 'basename $0' -> $0-current script that is running. Result is 'Script name: helloword.sh'
echo Script name: $0 -> Result is 'Script name: ./helloword.sh'. You can see the entire path


echo You are curently in the directory $PWD;echo And the current local time is 'date +%r'
RESULT:
You are currently in directory /root
And the current local time is 11:53:21 PM


you can include in a script call of another script like '. helloworldx.sh'
touch helloworldx.sh
pico helloworldx.sh
MESSAGE3="we are testing includes"
echo $MESSAGE3


if you run ./hellowworld.sh you obtain the RESULT
You are currently in directory /root
And the current local time is 11:53:21 PM
we are testing includes


head -n 1 helloworld,sh -> Returns the first line of the script


another method include in a script call of another script like 'source helloworldx.sh' -> The results is the same like the one above


./helloworld.sh | mail -s "testing hellow world using pipes" root ->helloworld.sh send the output to the root user by email
mutt -> utilitary to see emails

BASH : Quoting Nuances

2 types - strong quoting and normal quoting
echo this is a test
echo I want money $ -> prints 'I want money $'
echo I want money $sum -> prints 'I want money'
echo I want money \$sum -> prints 'I want money $sum' -> '\'-backslash
mkdir space directory -> create 2 directories
rm -rf space directory -> deletes the 2 above directories
mkdir space\ directory -> create 1 directorie
echo "this is a test $" -> print 'this is a test $'
echo "this is a test $1" -> print 'this is a test'
echo "this is a test $?" -> print 'this is a test 0'
echo 'this is a test $1' -> print 'this is a test $1'
echo 'this is a test $1 \' -> print 'this is a test $1 \'
echo "keep the backslash $ \\" -> prints 'keep the backslash $ \'

Sunday, January 30, 2011

FOREX : week 24 - 28 January 2011

AUD_USD
EUR_CHF

EUR_GBP

EUR_JPY

EUR_USD

GPY_JPY

GBP_USD

NZD_USD

USD_CAD

USD_CHF

USD_JPY

BASH : Command Substitution

Echo the content of a command stored in a variable or just make use of it. Special redirect.
Run a command with the output of another command.
' -> backtick
touch file_etc.txt 
vi file_etc.txt -> write '/etc' in the file and save it
ls -l 'cat file_etc.txt' -> output the content of /etc folder
ls -l 'cat file_etc.txt' | wc -l -> numeber of files
ls -l 'cat file_etc.txt' > etclist.txt -> redirect the /etc files into the file etclist.txt
1. etcdir='ls -l /etc' -> set a variable 'etcdir' that list the /etc folder
set | grep etcdir -> lists the files from 'etcdir' variable that displays /etc folder in an ugly way
echo "$etcdir" -> the format is nice now
2. etcdir1=$(ls -l /etc)
set | grep etcdir1
echo "$etcdir1" -> prints in a nice way
foldercount='ls -A | wc -l' 
echo $foldercount -> number of files
if $foldercount -gt 10 do something
pico file_etc.txt and add /bin folder
ls -l 'cat file_etc.txt' -> list the content of /etc and /bin in alfabetical order
du -h 'cat file_etc.txt' -> (du -disk utilization)  list the disk utilization for /bin and /etc folders for each file in part
netstat -ant | grep 443 -> monitor the process that run on port 443
sslstatus='netstat -ant | grep 443'
echo $sslstatus -> returns the number of processes runing on port 443
touch file.txt
echo line1 >> file.txt -> append a line to the file
echo line2 >> file.txt
filelist='< list.txt' -> '<'  redirection of input
echo $filelist -> print the content of the file on the same line

BASH : PIPES

Connect the output stream of command A to the input strem of a command B
ls -l | wc -l -> returns the number of files in a directory plus one( the . which is teh current direcotry). 
ls -A | wc -l -> returns the number of files in a directory. The output strem is redirected.
ls -l | sort -r -> return the file in reverse alfabetic order
ls -A | sort -r | wc -l -> returns the number of files in the folder
touch data
pico data -> write 2 colums separated by space;
CONTENT of file
firstname lastname
first last
---------------------
cat data -> display the data
cat data | cut -f 2 -d ' ' -> -f - field 2; -d - delimiter space
Returns:
lastname
last
---------
cp data data2 -> copy data to data2
pico data2 -> change the demiter to comma ','
cat data2 -> values in the file are delimited by comma ','
cat data2 | cut -f 1 - d ','
Returns:
firstname
first
--------------
cat data2 | cut -f 1 - d ',' | wc -> returns the 2 lines 2 words 14 caracters
ps -ax | grep httpd -> processes that contains streng 'httpd' are displayed
ps -ax |grep httpd | wc -l -> returns a number containing all the httpd processes plus the grep process
pipes are very fast, are done in memory not in disk
ps -ax -> this in done in memory

BASH : I/O Redirection

File descriptors 0,1 ,2 corresponds to keyboard, screen, standard error. By default every thing is redirected to screen. Standard input for computer is keyboard.
ls -l -> standard input is used (keyboard) and standard output displayes the results on monitor
Example:
touch file.txt
vi file.txt - type test in the file
cat < file.txt -> the content is displayed in the screen
ls -l /bin -> display the content of the /bin folder
grep test < file.txt -> search for text 'test' in file.txt
vi file.txt -> add another line 'second line'
grep test < file.txt -> Return the line containing the 'test' word
cat file.txt -> displays the containing text from the file
cat file.txt > file2.txt -> copy file.txt into file2.txt
cat file.txt >> file2.txt -> concatenate copy file.txt into file2.txt. The content is double, has the same content dubled
grep test < file.txt > file3.txt -> search text 'test' in file.txt and copy into file3.txt

BASH : Command Chaining and Command Lists

Command Chaining - multiple commands
'pwd; echo hello world' -> prints pwd and then prints 'hello world'
clear;pwd;echo hello world -> 3 commands in a row
clear;pwd;echo hello world;echo another line -> 4commands in a row
clear;cd /;ls -l;echo you are in $PWD;echo time to go back home;cd ~;echo you are now in $PWD; echo you were in $OLDPWD
set | grep $? -> list variables available


Command Lists: && - apersants; || - pipes
ls -l && pwd ->the second will execute only if the first returns status OK which is 0.
ls -l && echo you are now in $PWD
ls -l || echo you are now in $PWD -> the second will execute only if the first returns status FAILED which is 1.
ls -z || echo you are now in $PWD -> the echo will apear because the first command fails
ls -l && ls -z -> both runs and the second displays invalid option