shell笔记(一)——基本知识

1.把src文件夹下的所有名字中包含init字段的C文件拷贝到dsc目录里。

#!/bin/bash
echo "------start------"
cd ./src/
find -name "*.c" |grep  "init" |xargs -i cp {} ../dsc/

2.搜索当前目录下,列出所有包含"my"字段的C文件

find -name "*.c" -exec grep -l "my" {} \;

3.读取用户输入

#!/bin/bash
echo "what is your name?"
read name
echo "my name is:"$name

结果执行:

kldong@ubuntu:~/learning/shell/read$ ./read.sh 
what is your name?
kldong  (输入)
my name is:kldong

4.数组的创建——declare -a

#!/bin/bash
declare -a fruit=( apples pears plums )
echo ${fruit[0]}
运行结果:

kldong@ubuntu:~/learning/shell/arry$ ./array.sh 
apples

5.把UNIX/Linux命令的输出赋值给一个变量,可以通过使用反引号引用命令,还可以将该命令包含在由美元符号开始的一对圆括号中即可。

#!/bin/bash
path=`pwd`
echo $path;
echo "Today is $(date)";

执行结果:

kldong@ubuntu:~/learning/shell$ ./var.sh 
/home/kldong/learning/shell
Today is 2013年 07月 27日 星期六 15:46:43 CST
kldong@ubuntu:~/learning/shell$ vim var.sh

6.case 结构

#!/bin/bash
echo "please input!"
read my_fruit
case "$my_fruit" in
    apple)
       echo "you choose apple."
        ;;
    orange)
       echo "you choose orange"
        ;;
    pear)
       echo "you choose pear"
        ;;
    banana | grape)
       echo "you choose $my_fruit."
        ;;
    *)echo "we do not have the fruit you choosed."
        ;;
esac

运行结果:

kldong@ubuntu:~/learning/shell$ ./case.sh 
please input!
apple
you choose apple.
kldong@ubuntu:~/learning/shell$ ./case.sh 
please input!
grape
you choose grape.
kldong@ubuntu:~/learning/shell$ ./case.sh 
please input!
egg
we do not have the fruit you choosed.

7.if/else 分支语句

    if...else...语句可能会涉及到字符串比较、文件类型的判断,文件是否存在以及是否有可执行权限等。

7.1字符串的比较

=                          等于           if [ "$a" = "$b" ]
==                        与=等价
!=                         不等于        if [ "$a" = "$b" ]
<                          小于,在ASCII字母中的顺序:
                            if [[ "$a" < "$b" ]]
                            if [ "$a" \< "$b" ]         #需要对<进行转义
>                          大于

-z                         字符串为null,即长度为0
-n                         字符串不为null,即长度不为0


#!/bin/bash
echo "please slect the color!"
read color
if [ "$color" = "red" ] ;then
    echo "you choose red color"
elif [ "$color" = "green" ]; then
    echo "you choose green color"
elif [ "$color" =  "black" ];then
    echo "you choose green black";
else
    echo "do not have the color"
fi

运行结果:

kldong@ubuntu:~/learning/shell$ ./if_test.sh 
please slect the color!
red
you choose red color
kldong@ubuntu:~/learning/shell$ ./if_test.sh 
please slect the color!
white
do not have the color

7.2 二元比较操作符,比较变量或比较数字

-eq                       等于            if [ "$a" -eq "$b" ]
-ne                       不等于         if [ "$a" -ne "$b" ]
-gt                        大于            if [ "$a" -gt "$b" ]
-ge                       大于等于      if [ "$a" -ge "$b" ]
-lt                         小于            if [ "$a" -lt "$b" ]
-le                        小于等于      if [ "$a" -le "$b" ]

<                          小于(需要双括号)       (( "$a" < "$b" ))
<=                        小于等于(...)                (( "$a" <= "$b" ))
>                          大于(...)                      (( "$a" > "$b" ))
>=                        大于等于(...)                (( "$a" >= "$b" ))

#!/bin/bash
echo "please input the number!"
read number;
if (( "$number" > 2 ));then
    echo "greater than 2!"
elif [ "$number" -lt 2 ];then
    echo "less than 2"
else
    echo "equal with 2"
fi

运行结果:

kldong@ubuntu:~/learning/shell$ ./if_number.sh 
please input the number!
1
less than 2
kldong@ubuntu:~/learning/shell$ ./if_number.sh 
please input the number!
2
equal with 2
kldong@ubuntu:~/learning/shell$ ./if_number.sh 
please input the number!
3
greater than 2!
7.3 文件相关到判断

[ -f "somefile" ] :判断是否是一个文件
[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限
[ -n "$var" ] :判断$var变量是否有值
[ "$a" = "$b" ] :判断$a和$b是否相等
    -r file     用户可读为真
  -w file     用户可写为真
  -x file     用户可执行为真
  -f file     文件为正规文件为真
  -d file     文件为目录为真
  -c file     文件为字符特殊文件为真
  -b file     文件为块特殊文件为真
  -s file     文件大小非0时为真
  -t file     当文件描述符(默认为1)指定的设备为终端时为真 

#!/bin/bash
echo "to test permission!"
#ls -l for_test.sh
if [ -f "$1" ] ;then
    echo "$1 is a file."
else
    echo "$1 is not a file."
fi

运行结果:

kldong@ubuntu:~/learning/shell$ ./if_chmod.sh for_test.sh
to test permission!
for_test.sh is not a file.

kldong@ubuntu:~/learning/shell$ ./if_chmod.sh if_number.sh 
to test permission!
if_number.sh is a file.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值