Chinaunix首页 | 论坛 | 博客
  • 博客访问: 31071
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 131
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-14 19:23
个人简介

a person

文章分类

全部博文(11)

文章存档

2015年(11)

我的朋友

分类: LINUX

2015-05-22 21:37:17

Introduction


The default shell environment for most GNU/Linux system is 
Bash ( Bourne Again Shell ).
For any scripting languague in a Linux environment , a script start with a special line called shebang.
Shebang is a line for which #! is prefixed to the environment path. /bin/bash is the interpreter command path for Bash.

Execution of a script can be done in two ways. Either we can run the script as a command-line argument for sh or run a self execution with execution permission. for example:

点击(此处)折叠或打开

  1. sh script.sh

If a script is run as command-line argument for sh, the shebang in the script is of no use.

The execution permission for the script can be set as follows:

点击(此处)折叠或打开

  1. chmod +x script.sh

For self execution script. 

点击(此处)折叠或打开

  1. ./script.sh

In Bash, each command or command sequence is delimited by using a semicolon or a new line.

点击(此处)折叠或打开

  1. cmd1 ; cmd2
or

点击(此处)折叠或打开

  1. cmd1
  2. cmd2


Printing in the terminal


echo, printf,

点击(此处)折叠或打开

  1. echo $PATH #/usr/local/bin:/usr/sbin:/usr/bin
  2. echo "$PATH" #/user/local/bin:.....
  3. echo '$PATH' #$PATH

Note: when using echo with single quotes, the variable inside the quotes will not be interpreted by Bash, but will be display as is .

点击(此处)折叠或打开

  1. echo -e "1\t2\t3"
escaping new line in echo.

点击(此处)折叠或打开

  1. printf "%-5s -%-10s %-4s\n" No Name Mark


Playing with variable and environment variable


For every process , environment variables in its runtime can be viewed by:

点击(此处)折叠或打开

  1. cat /proc/$PID/environ

Set the PID with the process ID of the relevent process.
for example. assume that an application called gedit is running . We can obtain the progress ID of gedit with pgrep command as follows.

点击(此处)折叠或打开

  1. pgrep gedit
1250

点击(此处)折叠或打开

  1. cat /proc/1250/environ | tr '\0' '\n'

点击(此处)折叠或打开

  1. cat /proc/1250/environ | tr '\0' '\n'
Printing the content of a variable is done using by prefixing $ with the variable name as follows:

点击(此处)折叠或打开

  1. var="hello" #Assignment of value to variable var.
  2. echo $var
or

点击(此处)折叠或打开

  1. echo ${var}

There some default system environment variable:
SHELL, USER, HOME, PATH, UID and so on.
for example:

点击(此处)折叠或打开

  1. echo $SHELL # /bin/bash


Finding length of string


Get the length of a variable value as follow:

点击(此处)折叠或打开

  1. length=${#var}

for example:

点击(此处)折叠或打开

  1. var=12345
  2. echo ${#var} # 5


Check for super user


点击(此处)折叠或打开

  1. if [ $UID -ne 0 ]; then
  2. echo "Non root user. please run as root."
  3. else
  4. echo "Root user"
  5. fi


Doing math calculations with the shell


The bash shell environment can perform basic arithmatic operations using the commands  let, ( ( ) ) and [ ]. The two utilities expr and bc are very helpful performing advanced operations.

While using let, we use variable names without the $ prefix, for example:

点击(此处)折叠或打开

  1. n1=2;
  2. n2=3;
  3. let result=n1+n2;
  4. let result++;
  5. echo $result;

The [ ]    operator can be used similar to the let command as follows:

点击(此处)折叠或打开

  1. result=$[ n1 + n2 ]; # No space between = and $

(( )) can be alse used.

点击(此处)折叠或打开

  1. result=$(( n1 + n2 ));

expr can be alse used for basic operation.

点击(此处)折叠或打开

  1. result=`expr 3 + 4`
  2. result=$(expr $n1 + 5);

All of above methods do not support floating point numbers, and operate on integer only.

bc the precision calculator is an advanced utility for mathematical operations. It has a wide range of options.

点击(此处)折叠或打开

  1. echo "4 * 0.56" | bc
  2. result=`echo "$n1 * 1.5" | bc`


Arrays


点击(此处)折叠或打开

  1. array_var=(1 2 3 4)
  2. array_var[0]="test1"
  3. array_var[1]="test2"
  4. array_var[2]="test3"
  5. varay_var[3]="test4"
  6. echo ${array_var[0]};
  7. #echo $array_var[0]; # the usage is error.
  8. echo ${array_var[@]}; # print array length or ${array_var[*]}


Function and Arguments

A function can be defined as follows:

点击(此处)折叠或打开

  1. function fname(){
  2. statements;
  3. }
or

点击(此处)折叠或打开

  1. fname(){
  2. statements;
  3. }


$1 is the first argument
$n is the n argument
"$@“ expands as "$1" "$2" "$3" and so on
"$*"   expands as "$1c$2c$3", where c is the first character of IFS
”$@" is the most use one.

Store command output

cmd_out=$(COMMAND)
or
cmd_out=`ls | cat -n `

for example:

点击(此处)折叠或打开

  1. cmd_out=$(ls | cat -n)
  2. cmd_out=`ls | cat -n`

Spawning a separate process with subshell


Subshell are separate processes. A subshell can be defined using the ( ) operator as follows:

点击(此处)折叠或打开

  1. pwd;
  2. (cd /bin; ls)
  3. pwd;

Read input from Teriminal

read -n number-of-chars variable_name

Read a password in non-echoed mode:

点击(此处)折叠或打开

  1. read -s var
  2. echo $var;

Display a message with read using:

点击(此处)折叠或打开

  1. read -p "Enter input:" var
  2. echo $var

Loop


点击(此处)折叠或打开

  1. for var in {a..z}
  2. do
  3. echo $var;
  4. done


or

点击(此处)折叠或打开

  1. for((i=0; i<10; i++)){
  2. echo $i | tr '\n' ' '
  3. }


点击(此处)折叠或打开

  1. i=10;
  2. while [ $i -gt 0 ]
  3. do
  4.  let i--;
  5.  echo $i;
  6. done


Flow Control and tests

if condition:

点击(此处)折叠或打开

  1. if condition;
  2. then
  3.  commands;
  4. fi

if else condition

点击(此处)折叠或打开

  1. if condition;
  2. then
  3.  commands;
  4. elif condition
  5. then
  6.  commands;
  7. else
  8.  commands;
  9. fi

comparisions:
[ $var -eq 0 ]  
Note that there is a space between [  or  ]  and operands. 
Multiple test condition can be combined as follows:

点击(此处)折叠或打开

  1. [ $var -ne 0 -a $var2 -gt 2 ] # using AND -a
  2. [ $var -ne 0 -o $var2 -gt 2 ] # OR -o

Import operators are :
-gt:      great than
-lt        less than
-ge      great than or equal to 
-le       less than or equal to 
Filesystem related test:
-f       if a file.
-x      if file is executable.
-d      if directory.
-e      if file is existed.
-c      if file is character device.
-b      if file is block device.
-w     if file is writable.
-r      if file is readable.
-L      if file is a symlink.


String comparision


点击(此处)折叠或打开

  1. [[ $str1 == $str2 ]]; # compare two string
  2. [[ -z $str ]]; # Return true if str holds an empty string.
  3. [[ -n $str ]]; # Return true if str holds non-empty string.


for example:

点击(此处)折叠或打开

  1. if [[ -n $str1 ]] && [[ -z $str2 ]];
  2. then
  3.  commands;
  4. fi

Slicing filenames based on extension

example 1:

点击(此处)折叠或打开

  1. filename="sample.jpg";
  2. name=${filename%.*}
  3. echo File name is $name. # the output is sample
  4.  
  5. extension=${filename#*.};
  6. echo extension is $extension # the output is jpg
  7.  
  8. var=hack.fun.book.txt
  9. echo ${var%%.*} # output is hack. because %% is greedy
  10. echo ${var##*.} # output is txt. because ## is greedy

Rename file example:

点击(此处)折叠或打开

  1. count=1;
  2. for f in *.txt
  3. do
  4.  newname=${f%%.*}-$count.${f##*.}
  5.  mv "$f" "$newname" 2>/dev/null
  6.  if [ $? -eq 0 ];
  7.  then
  8.  echo "Renaming $f to $newname"
  9.  let count++;
  10.  fi
  11. done



阅读(1086) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~