Posts

Showing posts from June, 2015

how to install custom deb package

To install a downloaded Debian ( Ubuntu ) package (.deb): Open Terminal and type sudo dpkg -i packagename.deb To remove a Debian (Ubuntu) package (.deb): sudo dpkg -r packagename To Reconfigure/Repair an installed Debian (Ubuntu) package (.deb): sudo dpkg-reconfigure packagename

bash file exists

 cat conditf #!/bin/bash FILE=$1 if [ _ -f $FILE _ ]; then    echo "File $FILE exists." else    echo "File $FILE does not exist." fi ./conditf sc File sc does not exist. ./conditf sc3 File sc3 exists. # -f file - True if file exists and is a regular file.

scp

On ubuntu/debian for example, the openssh-client package provides the scp program. $ dpkg -L openssh-client | grep scp /usr/bin/scp /usr/share/man/man1/scp.1.gz So if you are "sending" file from your local machine to a remote machine (uploading) the syntax would look like this $ scp ~/my_local_file.txt user@remote_host.com:/some/remote/directory When copying file from remote host to local host (downloading), its looks just the reverse $ scp user@remote_host.com:/some/remote/directory ~/my_local_file.txt The verbose output would then indicate the exact point where the program ran into issues. $ scp -v ~/test.txt root@192.168.1.3:/root/help2356.txt Multiple files can be specified separated by a space like this $ scp foo.txt bar.txt username@remotehost:/path/directory/ To copy an entire directory from one host to another use the r switch and specify the directory $ scp -v -r ~/Downloads root@192.168.1.3:/root/Downloads Scp can copy files from 1 r

bash loops | for | while | until

FOR loop for i in {a..z}; do actions; done; ------------ #!/bin/bash for i in {a..z}; do echo $i; done -- ./sc2 a ... z ----- cat sc2 #!/bin/bash for i in {1..4} do echo $i done $ ./sc2 1 2 3 4 $ --------------- The for loop can also take the format of the for loop in C. For example: for((i=0;i<10;i++)) { commands; # Use $i } cat sc2 #!/bin/bash for ((i=0;i<10;i++)){ echo $i } ------ 0 1 ...... 8 9 --------------------- WHILE cat sc3 #!/bin/bash i=0; while [ $i -lt 9 ]  #SPACE after and before [ do echo $i i=$(($i+1)) done - ./sc3 0..8 Alternative increment cat sc3 #!/bin/bash i=0; while [ $i -lt 9 ] do let i++ echo $i done 1....9 ---------- most common conditions ‘ -eq ’, ‘ -ne ’, ‘ -lt ’, ‘ -le ’, ‘ -gt ’, or ‘ -ge ’ --------------- UNTIL  cat until #!/bin/bash x=0 until [ $x -eq 9 ]; # [ $x -eq 9 ] is the condition do let x++ echo $x done 1....9

functions

fname() { > echo test > } $ fname test

debugging scripts | set -x | _DEBUG="on"

Image
set -x : Displays arguments and commands upon their executio set +x : Disables debugging #!/bin/bash #Filename:script.sh for i in {1..4} do set -x echo $i set +x done ---- + echo {1..4} {1..4} + set +x ------------------- Shebang hack The shebang can be changed from #!/bin/bash to #!/bin/bash –xv to enable debugging without any additional flags (-xv flags themselves). ----------------- Use of intelligent DEBUG function First, add a special variable called _DEBUG . Set _DEBUG to ‘on’ when you need to debug a script: _DEBUG="on" Put the following function at the beginning of the script: function DEBUG ( ) { [ " $_DEBUG " == "on" ] && $ @ } Now wherever you need debugging simply use the DEBUG function as follows: DEBUG echo "File is $filename" OR DEBUG set -x Cmd1 Cmd2 DEBUG set +x When done with debugging (and before moving your script to production) set _DEBUG to ‘off’. No need to delete d

aliases | how to ignore aliases command

 We can ignore any aliases currently defined by escaping the command we want to run. For example: $ \command The \ character escapes the command, running it without any aliased changes

variables

svar=123 echo "the value is ${svar}" the value is 123 echo "the value is $svar" the value is 123 finding length of string: echo "the value is ${#svar}" the value is 3  ------------------------ no1=4 no2=5 let res=no1+no2 echo $res 9 g=no1+no2 echo $g no1+no2 let res2=no1+no2 echo $res2 9 -----------------------  You can redirect stderr exclusively to a file and stdout to another file as follows: $ cmd 2>stderr.txt 1>stdout.txt ------------------ Creating custom file descriptors, as 0,1,2 stdin,out,err echo this is a test line > input exec 3<input cat <&3 this is a test line ---------------------------- arrays f[0]="abc" f[1]="def" echo ${f[0]} abc Show full arrays elements echo ${f[*]} abc def show arrays length: echo ${f[*]} abc def 2 ----- Show declared variables declare -p | tail -7 declare -a f='([0]="abc" [1]="def")' declare -- g=

processes

 ps   PID TTY          TIME CMD  3395 pts/13   00:00:00 bash 11087 pts/13   00:00:00 ps ubuntu2@ubuntu2:~$ ll /proc | grep 3395 dr-xr-xr-x   9 ubuntu2    ubuntu2             0 Jun 17 02:03 3395/ /proc/3395/environ -r-------- 1 ubuntu2 ubuntu2 0 Jun 21 01:08 /proc/3395/environ You can obtain the environment variables associated with the process by executing the following command: cat environ XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdgLANG=en_US.UTF-8SHLVL=0XDG_VTNR=7COMPIZ_CONFIG_PROFILE=ubuntuLANGUAGE=en_USMANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.pathLOGNAME=ubuntu2PWD=/home/ubuntu2GNOME_KEYRING_PID=2846XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/ubuntu2DISPLAY=:0QT_QPA_PLATFORMTHEME=appmenu-qt5GTK_IM_MODULE=ibusSELINUX_INIT=YESXDG_SESSION_ID=c2XAUTHORITY=/home/ubuntu2/.XauthorityDESKTOP_SESSION=ubuntuIM_CONFIG_PHASE=1DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.pathGDMSESSION=ubuntuGNOME_

colored text in bassh

echo -e "\e[1;31m This is red text \e[0m"  This is red text echo -e "\e[1;32m This is red text \e[0m"  This is red text echo -e "\e[1;33m This is red text \e[0m"   This is red text