linux shellscripts 基本用法

//#########################################################################
#tutorial primary usage of shellscripts                                 #
//#########################################################################

//#########################################################################
1、read [pt] variable
//此cmd可以有提示符和限定的时间从控制台让你输入一个字符串然后将这个字符窜
//赋值给这个variable
//example:

[zyb@zyb1 scripts]$ read -p "put your name here:" -t 3 name
put your name here:zhangsan
[zyb@zyb1 scripts]$ echo $name
zhangsan

//#########################################################################

//#########################################################################
2、test [...]这个东东有很多的options,它主要是判断linux内部文件类型和文件
//是否存在
[zyb@zyb1 scripts]$ test -e /etc/ -a -e /etc && echo "succeed"
succeed

//解析:
//-e 表示当前这个文件必须存
//-a 表示and关系也就是这两个-test的返回值必须为真的时候才可以输出succed
//&& 是shell里面一个具有特殊功能的东东,它的意思是说当前一个命令执行成功才会执行
//后边这个 ||这个符号正好相反

//#########################################################################


//#########################################################################
3、how to transport number of parameter to shellscripts(怎么给shellscripts传输几个参数)
[zyb@zyb1 scripts]$ cat -n ./sho1.sh /*
     1  #!/bin/bash
     2
     3
     4
     5  #read -p "put your firstname hear" firstname
     6  #read -p "put your lastname hear" lastname
     7  #echo "full name is $firstname $lastname"
     8
     9
    10  #test -e /testi
    11  #test -e /etc echo "yes" -a test -e /ets echo "no";
    12
    13  echo "show input parameters:"
    14  echo "\$\# represent many number of parameters but exclude file name:$#"
    15  echo "\$0 represent the current execute shellscripts's file name:$0";
    16  echo "\$@ represent the transport parameters:$@"
    17  echo "\$* star repsent index of parameter's:$1"
    18  exit 0
    19
    20 */
[zyb@zyb1 scripts]$ ./sho1.sh
show input parameters:
$\# represent many number of parameters but exclude file name:0
$0 represent the current execute shellscripts's file name:./sho1.sh
$@ represent the transport parameters:
$* star repsent index of parameter's:
#########################################################################


//#########################################################################
4、shellscripts的条件语句

//多重if
[zyb@zyb1 scripts]$ cat -n ./ifthen.sh;./ifthen.sh; /*
     1  #!/bin/bash
     2  read -p " show your decide y/n:" -t 3 anwser
     3  #echo "anwser:$anwser"
     4  if [ "$anwser" == 'y' ] || [ "$anwser" == 'Y' ];
     5  then
     6     echo "ok!!!"
     7     exit 0
     8  #elif [ "$anwser" > "1" ]; then echo "111!!!" exit 0
     9  elif [ "$anwser" == "e" ]; then echo "222!!!" exit 0
    10  else
    11     echo -e "\n no!!!"
    12  fi
    13
    14
    15
    16
 show your decide y/n:n

 no!!!*/


//case语句
[zyb@zyb1 scripts]$ cat -n ./case.sh;./case.sh ; /*
     1  #!/bin/bash
     2  case $1 in
     3     "hello")
     4      echo "hello";;
     5     "")
     6      echo "null";;
     7     *)
     8      echo "hello else";;
     9  esac;
null */



//函数定义
[zyb@zyb1 scripts]$ cat -n ./shellfunction.sh;./shellfunction.sh ;/*
     1  #!/bin/bash
     2  function tst(){
     3   echo -e "\n test $@"
     4  }
     5  tst 11 22;
     6

 test 11 22 */



//while循环
[zyb@zyb1 scripts]$ cat -n ./while.sh;./while.sh /*
     1  #/bin/bash
     2  function f1(){
     3  read -p "put y/n:" yn
     4  }
     5  f1
     6
     7  while [ "$yn" == "y" ]
     8  do
     9   f1
    10  done
    11  echo "over"
put y/n:y
put y/n:y
put y/n:y
put y/n:y
put y/n:n
over
*/


//for 循环
[zyb@zyb1 scripts]$ cat -n ./for.sh;./for.sh /*
     1  #/bin/bash
     2  for var in $@
     3  do
     4   echo ${var}
     5  done
     6
     7  for var in $(seq 1 5)
     8  do
     9   echo "number${var}"
    10  done
    11
    12  s=0
    13  for (( i=0;i<5;i++ ))
    14  do
    15   s=$(( $s+$i ))
    16  done
    17
    18  echo "sum is:$s"
number1
number2
number3
number4
number5
sum is:10
*/

//#########################################################################










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值