设为首页 收藏本站
查看: 783|回复: 0

Shell Cookbook

[复制链接]

尚未签到

发表于 2015-12-15 14:54:28 | 显示全部楼层 |阅读模式
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:




  • 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:




  • chmod +x script.sh



For self execution script.




  • ./script.sh




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




  • cmd1 ; cmd2


or



  • cmd1
  • cmd2



Printing in the terminal



echo, printf,




  • echo $PATH #/usr/local/bin:/usr/sbin:/usr/bin
  • echo "$PATH" #/user/local/bin:.....
  • 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 .




  • echo -e "1\t2\t3"


escaping new line in echo.




  • 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:




  • 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.




  • pgrep gedit


1250




  • cat /proc/1250/environ | tr '\0' '\n'




  • cat /proc/1250/environ | tr '\0' '\n'

Printing the content of a variable is done using by prefixing $ with the variable name as follows:




  • var="hello"  #Assignment of value to variable var.
  • echo $var


or



  • echo ${var}



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




  • echo $SHELL # /bin/bash


Finding length of string



Get the length of a variable value as follow:




  • length=${#var}



for example:




  • var=12345
  • echo ${#var} # 5


Check for super user






  • if [ $UID -ne 0 ]; then
  • echo "Non root user. please run as root."
  • else
  • echo "Root user"
  • 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:




  • n1=2;
  • n2=3;
  • let result=n1+n2;
  • let result++;
  • echo $result;



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




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



(( )) can be alse used.




  • result=$(( n1 + n2 ));



expr can be alse used for basic operation.




  • result=`expr 3 + 4`
  • 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.




  • echo "4 * 0.56" | bc

  • result=`echo "$n1 * 1.5" | bc`



Arrays





  • array_var=(1 2 3 4)
  • array_var[0]="test1"
  • array_var[1]="test2"
  • array_var[2]="test3"
  • varay_var[3]="test4"

  • echo ${array_var[0]};
  • #echo $array_var[0];  # the usage is error.

  • echo ${array_var[@]}; # print array length  or  ${array_var
  • }


Function and Arguments

A function can be defined as follows:




  • function fname(){
  • statements;
  • }

or




  • fname(){
  • statements;
  • }





$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:




  • cmd_out=$(ls | cat -n)
  • 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:




  • pwd;
  • (cd /bin; ls)
  • pwd;


Read input from Teriminal

read -n number-of-chars variable_name


Read a password in non-echoed mode:




  • read -s var
  • echo $var;



Display a message with read using:




  • read -p "Enter input:" var
  • echo $var


for var in {a..z}
do
echo $var;
done




or




  • for((i=0; i/dev/null
  • if [ $? -eq 0 ];
  • then
  • echo "Renaming $f to $newname"
  • let count++;
  • fi
  • done

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-151637-1-1.html 上篇帖子: shell中如何判断一个变量是否为空 下篇帖子: 【安全健行】(3):Shellcode基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表