Shell 编程

!!!!!!!!!!!!!!!!注意空格不可以随便用额,会恶心屎你的。。。

**************************************
如何计算传递进来的参数 ?

    $#

 ***************************
如何在脚本中获取脚本名称 ?

    $0

 ******************************************
如何检查之前的命令是否运行成功 ?

    $?
0为真 1 为假

****************************************
每个脚本开始的 #!/bin/sh 或 #!/bin/bash 表示什么意思 ?

这一行说明要使用的 shell。#!/bin/bash 表示脚本使用 /bin/bash。对于 python 脚本,就是 #!/usr/bin/python。(LCTT译注:这一行称之为释伴行。)

 
********************************88
判断文件是否存在
 
if [ -f filename ]
then
fi
**********************************
整数加法

declare -i a=3 b=4  
declare -i c=$a+$b
echo $c

-i integer
-a array
-f function
-r readonly

*************************************************
for循环

1.
for i in `seq 10`
  do
     echo $i
  done

2.
for ((i=0;i<10;i++))
   do
     echo $i
   done

注意:2比1要好,因为seq对于大数就挂了,会跑死的,建议用第二种格式
************************************************8
while 循环

i=0
while($i<10)
do
   echo $i
   i=$(($i+1))
done


********************************
函数
#!/bin/bash
function fun()
{
   for name in $@
      do
        echo "Hello ${name}"
      done
}

fun xx yy

*****************************************************
写出输出数字 0 到 100 中 3 的倍数(0 3 6 9 …)的命令 ?

    for i in{0..100..3};do echo $i;done



    for(( i=0; i<=100; i=i+3));do echo "Welcome $i times";done


************************************
 [ $a == $b ] 和 [ $a -eq $b ] 有什么区别?

    [ $a == $b ] - 用于字符串比较
    [ $a -eq $b ] - 用于数字比较


8*********************************************
如何打印数组的所有元素 ?

    echo ${array[@]}

*******************************************
如何移除数组中索引为 2 的元素 ?

    unset array[2]

****************************

read 接受用户输入

read -p "plese input a string: " str
>hello world
echo $str
<hello world


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值