linux编译shell,Linux shell基本脚本编译

使用shell打印日期和登录到当前系统的用户

#直接在命令行打印当前日期

$ date

2020年 6月13日 星期六 10时49分22秒 CST

#查看当前谁登陆了系统

$ who

a console Jun 8 19:10

a ttys000 Jun 8 19:10

使用shell来执行上面的命令

1.新建shell脚本文件

$ vim test1

2.编写脚本内容

#!/bin/bash

echo The time and date are:

date

echo "Let's see who's logged into the system:"

who

3.运行脚本

$ ./test1

-bash: ./e: Permission denied

#该文件没有执行权限,查看文件权限

$ ls -l test1

-rw-r--r-- 1 a staff 445 6 13 10:56 test1

#给文件增加执行权限

$ chmod 777 test1

#再次查看权限

$ ls -l test1

-rwxrwxrwx 1 a staff 445 6 13 10:56 test1

权限参考

dc230329c3a9

chmod 权限

#有执行权限了,再次执行脚本

$ ./test1

The time and date are:

2020年 6月13日 星期六 11时03分04秒 CST

Let's see who's logged into the system:

a console Jun 8 19:10

a ttys000 Jun 8 19:10

使用$符号打印变量

#查看一下系统的变量

$ set

HOME=/Users/a

HOSTNAME=appledeMacBook-Pro-4.local

HOSTTYPE=x86_64

LANG=zh_CN.UTF-8

#编写新脚本

$ vim test2

1.编辑内容,使用$打印环境变量

#!/bin/bash

echo "User info for userid: $USER"

echo UID: $UID

echo HOME: $HOME

2.运行

$ ./test2

User info for userid: root

UID: 0

HOME: /Users/a

也可以自定义变量打印

$ vim test3

#编辑内容为如下:

#!/bin/bash

days=10

guest="Tom"

echo "$guest checked in $days days ago"

days=5

guest="Jack"

echo "$guest checked in $days days ago"

#运行

$ chmod 777 test3

$ ./test3

Tom checked in 10 days ago

Jack checked in 5 days ago

传递变量

$ cat test4

#!/bin/bash

value1=10

value2=$value1

echo The resulting value is $value2

$ ./test4

The resulting value is 10

使用反引号,将shell命令赋予变量

$ cat test5

#!/bin/bash

testing=`date`

echo "The date and time are: " $testing

$ ./test5

The date and time are: Sat Jun 13 16:29:54 CST 2020

利用反引号,设置记录带有日期的日志文件

$ cat test5

#!/bin/bash

testing=`date`

echo "The date and time are: " $testing

today=`date +%y%m%d`

ls ~/shell -al > log.$today

$ ./test5

The date and time are: Sat Jun 13 16:53:25 CST 2020

$ cat log.200613

total 28

drwxrwxrwx 2 root root 4096 Jun 13 16:53 .

dr-xr-x---. 11 root root 4096 Jun 13 16:53 ..

-rwxrwxrwx 1 root root 0 Jun 13 16:53 log.200613

-rwxrwxrwx 1 root root 7 Jun 13 16:52 test1

-rwxrwxrwx 1 root root 79 Jun 13 14:17 test2

-rwxrwxrwx 1 root root 132 Jun 13 15:58 test3

-rwxrwxrwx 1 root root 61 Jun 13 16:26 test4

-rwxrwxrwx 1 root root 117 Jun 13 16:52 test5

重定向输出输入

使用符号大于号 > 将日期内容重定向输出到test6文件,如果文件不存在会自动创建一个

$ date > test6

$ ls -l test6

-rw-r--r-- 1 root root 29 Jun 13 16:58 test6

$ cat test6

Sat Jun 13 16:58:14 CST 2020

再次重定向输出会覆盖原来的内容

$ who > test6

$ cat test6

a console Jun 8 19:10

a ttys000 Jun 8 19:10

如果需要追加内容,使用两个大于号 >>

$ date >> test6

$ cat test6

a console Jun 8 19:10

a ttys000 Jun 8 19:10

Sat Jun 13 17:05:05 CST 2020

使用小于号 < 将内容重定向输入给shell命令

wc命令对文本进行计数,默认为以下3个值:

文本行数

文本单词数

文本字节数

$ wc < test6

3 17 116

内置输入重定向 两个小于号 <<

EOF为标记当前文本的开头和结尾

文本输入会一直继续提示到你输入标记结尾的标识为止,这里我自定义了EOF为文本结尾

$ wc << EOF

> hello world

> hahahaha

> 123456

> EOF

3 4 28

使用管道,将命令传送给另一个命令

- 输出已安装软件包的列表

$ rpm -qa > rpm.list

$ sort < rpm.list

GeoIP-1.5.0-14.el7.x86_64

NetworkManager-1.18.0-5.el7_7.1.x86_64

NetworkManager-libnm-1.18.0-5.el7_7.1.x86_64

NetworkManager-team-1.18.0-5.el7_7.1.x86_64

NetworkManager-tui-1.18.0-5.el7_7.1.x86_64

PyYAML-3.10-11.el7.x86_64

abrt-2.1.11-55.el7.centos.x86_64

使用管道完成上面的操作

$ rpm -qa | sort > rpm.list

$ cat rpm.list

GeoIP-1.5.0-14.el7.x86_64

NetworkManager-1.18.0-5.el7_7.1.x86_64

NetworkManager-libnm-1.18.0-5.el7_7.1.x86_64

NetworkManager-team-1.18.0-5.el7_7.1.x86_64

NetworkManager-tui-1.18.0-5.el7_7.1.x86_64

PyYAML-3.10-11.el7.x86_64

abrt-2.1.11-55.el7.centos.x86_64

由于上面的sort输出的数据急剧增大,所以可以加上 less或者more,将输出强制停留在每一个屏幕上

$ rpm -qa | sort | more

GeoIP-1.5.0-14.el7.x86_64

NetworkManager-1.18.0-5.el7_7.1.x86_64

NetworkManager-libnm-1.18.0-5.el7_7.1.x86_64

NetworkManager-team-1.18.0-5.el7_7.1.x86_64

NetworkManager-tui-1.18.0-5.el7_7.1.x86_64

PyYAML-3.10-11.el7.x86_64

abrt-2.1.11-55.el7.centos.x86_64

--More--

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值