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

本文介绍了多个Shell脚本实例,包括文件操作、条件判断、循环结构等,通过具体场景展示了如何利用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.



随着信息技术在管理上越来越深入而广泛的应用,作为学校以及些培训机构,都在用信息化战术来部署线上学习以及线上考试,可以与线下的考试有机的结合在起,实现基于SSM的小码创客教育教学资源库的设计与实现在技术上已成熟。本文介绍了基于SSM的小码创客教育教学资源库的设计与实现的开发全过程。通过分析企业对于基于SSM的小码创客教育教学资源库的设计与实现的需求,创建了个计算机管理基于SSM的小码创客教育教学资源库的设计与实现的方案。文章介绍了基于SSM的小码创客教育教学资源库的设计与实现的系统分析部分,包括可行性分析等,系统设计部分主要介绍了系统功能设计和数据库设计。 本基于SSM的小码创客教育教学资源库的设计与实现有管理员,校长,教师,学员四个角色。管理员可以管理校长,教师,学员等基本信息,校长角色除了校长管理之外,其他管理员可以操作的校长角色都可以操作。教师可以发布论坛,课件,视频,作业,学员可以查看和下载所有发布的信息,还可以上传作业。因而具有定的实用性。 本站是个B/S模式系统,采用Java的SSM框架作为开发技术,MYSQL数据库设计开发,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得基于SSM的小码创客教育教学资源库的设计与实现管理工作系统化、规范化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值