Color console
reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, and white=37
Coloured background
reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47
Usage
echo -e "\e[1;XXm Green Background \e[0m"
replace XX with color code
Printf formatting
reset=0, black=30, red=31, green=32, yellow=33, blue=34, magenta=35, cyan=36, and white=37
Coloured background
reset = 0, black = 40, red = 41, green = 42, yellow = 43, blue = 44, magenta = 45, cyan = 46, and white=47
Usage
echo -e "\e[1;XXm Green Background \e[0m"
replace XX with color code
Printf formatting
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
- left align, 5 is the width
Obtain process id of a running application
pgrep gedit
Get env variables for process
cat /proc/12501/environ | tr '\0' '\n
tr command will replace null with newline
Setting env/user variables
export HTTP_PROXY=http://192.168.0.2:3128
Length of variable
length=${#var}
Identify Current shell
echo $SHELL or echo $0
Current user id
$UID
The UID for the root user is 0.
Change the prompt
Modify the $PS1 in the bashrc file in the home directory
Eg: PS1='\[`[ $? = 0 ] && X=2 || X=1; tput setaf $X`\]\h\[`tput sgr0`\]:$PWD\n\$ '
Delete line on vi
dd
Calculations in shell script (Integers)
let result=no1+no2 echo $result
Not let command need to pass variables without $ sign
Or
result=$[ $no1 + no2 ]
result=$(( $no1 + no2 ))
using 4 sign inside is permitted for these options
Or
result=`expr 3 + 4` result=$(expr $no1 + 5)
Floating point calculations
echo "4 * 0.56" | bc
result=`echo "$no * 1.5" | bc`
echo "scale=2;3/8" | bc
File Descriptors
0 - stdin
1 - stdout
2 - stderr
> truncate the file
>> append to a file
Redirecting to specific descriptor Eg:-
1 is default
2> or 2>>
Transfer stderr and stdout to single file
cmd 2>&1 output.txt
OR
cmd &> output.txt
Ignore output
redirect to /dev/null
Tee command direct stdout to text and also display on screen
cat a* | tee –a out.txt | cat –n.
-a append the data
Regular and Associative array
Regular arrays are arrays which can use only integers as its array index. But associative arrays are arrays which can take a string as its array index.
declare -A ass_array
Alias
Shortcut for an actual command
alias rm='cp $@ ~/backup; rm $@'
Get return value of last command
$? will give the return value of the command
Functions can be exported like variables
Back quote ( ` ) ~ key
Can be used to store output of a command
cmd_output=`ls | cat -n`
Note:- single quote (') will save the command itself.
Subshell
executing commands in ( ) will not affect the current shell.
pwd; (cd /bin; ls); pwd;
Read command
Accepts user input.
Shortcut conditions
[ condition ] && action; # action executes if condition is true. [ condition ] || action; # action executes if condition is false.
Filesystem tests
-f is a filepath or file
-x is an executable
-d is a directory
-e is an existing file
-w is a writable file
-r is a readable file
-L is a symbolic link
Eg:-
fpath="/etc/passwd" if [ -e $fpath ]; then echo File exists; else echo Does not exist; fi
String comparisions:
Best to use [[double square brackets]] while comparing
[[ a = b ]] //Comparision. Space is needed
[[a=b]] //Assignment
-z : Empty string
-n : non empty string
test command
Square brackets can be eliminated by appending command to test command
if test $var -eq 0 ; then echo "True"; fi
No comments:
Post a Comment