linux shell脚本编程基础实验,Linux学习-shell脚本编程基础(节选)

第一记

1、  简单的helloworld编写

Shell输入下输入命令:vi helloworld.sh

随后进入文本编辑:

#!/bin/shell

#this is ahelloworld test

a=”helloworld”

echo $a

执行helloworld.sh文件

命令:

# sh helloworld.sh

2、变量赋值的方式是

# variable_name = variable_value

如果对一个已经有值的变量赋值,新值将取代旧值。取值的时候要在变量名前加$,$variable_name可以在引号中使用,如果出现混淆的情况,可以使用花括号来区分,例如:

# echo "Hi, $as"

就不会输出“Hi, helloworlds”,而是输出“Hi,”。这是因为Shell把$as当成一个变量,而$as未被赋值,其值为空。正确的方法是:

$ echo "Hi, ${a}s"

单引号中的变量不会进行变量替换操作。

3.who 命令查看有谁登陆系统

who| wc –l   计算用户总数

4.cat > nusers 建立文件nusers,使用cat复制终端的输入

echo “haha”

然后 ctrl+d表示end of file

使用chmod +x nesers 让文件拥有执行的权限

测试:./nusers

shell识别三种命令:内建命令、shell函数以及外部命令

5.printf和echo的区别

printf不像echo那样能够自动换行

6.重定向与管道

7.shell命令

# who 查看有谁登陆

# date查看当前日期

# chmod +x file修改file文件,让其成为可执行文件

# echo 显示文本字符串内容

# cat file显示file文件的内容,和more差不多

# ./file执行file文件

# set显示完整的环境变量配置列表

# testing=`date `反引号的作用:输出值;此处吧date值输出并赋值给testing

# $testing$用于输出变量的值

# command > outputfile将command命令的结果写入到outputfile文件中(覆盖式写入)

# command >> outputfile将command命令的结果写入到outputfile文件中(追加式写入)

# wc << EOF提示符一直提示输入数据,知道输入EOF,然后wc命令开始对内联输入重定向提供的数据执行行,词和字节计数。

# rpm –qa > rpm.list将已经安装的软件包列表数据输入到rpm.list

# sort rpm.list排序rpm.list

# rpm –qa | sort通过管道|将两条命令合成一条

# expr 1 + 5expr执行加法运算,结果为6

#bc进行浮点数运算命令

# $?查看最后一条命令的退出状态码

第二记

1.for命令 格式:

for var in list

do

commands

done

写一个用for遍历list的程序,如下:

[root@master test]# vi sh1201

#!/bin/sh

for i in liudiwei xuxu xiao liang hao

do

echo "this is my friend $i"

done测试

[root@master test]# ./sh1201

this is my friend liudiwei

this is my friend xuxu

this is my friend xiao

this is my friend liang

this is my friend hao

如果list里面有单引号,如I’m a student.

可以使用两种方式来解决:

(1)      使用转义字符  I\’m a student

(2)      使用双引号来定义用到单引号的值。”I’m” a student.

2.while命令

while命令的格式:

while test command

do

other commands

done

编写testwhile

[root@master test]# vi testwhile

#!/bin/sh

i=0

while [ $i -le 10 ]

do

echo "i=$i"

i=$[ $i + 1 ]

done

测试:

[root@master test]# ./testwhile

i=0

i=1

i=2

i=3

i=4

i=5

i=6

i=7

i=8

i=9

i=10

3.util命令

命令格式

until test commands

do

other commands

done

说明:直到  commands为真,不然就一直执行other commands.

4.嵌套循环输出九九乘法表

编辑一个新文件

[root@master test]# vi mutil99

九九乘法表代码:

#/bin/sh

for (( i=1;i<=9;i++))

do

for ((j=1;j<=i;j++))

do

echo -n "$j*$i=$[$i*$j] "

done

echo

done

结果:

[root@master test]# ./mutil99

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

1*5=5 2*5=10 3*5=15 4*5=20 5*5=25

1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36

1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49

1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64

1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

5.break和continue

6.处理循环输出

循环遍历文件夹并判断是一个目录还是一个文件

[root@master test]# vi isdirorfile

#/bin/sh

for file in /root/*

do

if [ -d "$file" ]

then

echo "$file is a directory!"

else

echo "$file is a file!"

fi

done > output.txt #讲结果输出到output.txt文件里

结果

[root@master test]# more output.txt

/root/anaconda-ks.cfg is a file!

/root/dead.letter is a file!

/root/downloads is a directory!

/root/hadoop is a directory!

/root/hello.sh is a file!

/root/initial-setup-ks.cfg is a file!

/root/src is a directory!

/root/test is a directory!

/root/testq is a file!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值