linux shell 命令引用,Linux5+6 shell命令和引用

本文档展示了Linux命令行的基本操作,包括文件管理、统计行数、定义变量及文件重命名。通过实例解释了如何使用`who`, `wc`, `chmod`, `cat`等命令,并演示了编写简单的Linux脚本程序,如`nf`和`whos`,用于统计当前目录的文件数和显示已登录用户列表。同时,提到了参数传递、字符串处理和日期时间的获取。
摘要由CSDN通过智能技术生成

[root@biao LinuxTest]# who |wc -l         //wc -l是统计行数。2chmod //修改文件的属性[root@biao LinuxTest]# vi nu创建文件并输入:who |wc -l

[root@biao LinuxTest]# cat nu

who |wc -l

[root@biao LinuxTest]# chmod +x nu

[root@biao LinuxTest]# ./nu

2

[root@biao LinuxTest]# command=wc   //定义变量。[root@biao LinuxTest]# option=-l

[root@biao LinuxTest]# file=grade

[root@biao LinuxTest]# $command $option $file#等同于wc -l grade即统计grade的行数5 grade空值[root@biao LinuxTest]# echo :$nothing:

::

[root@biao LinuxTest]# wc $nothing -l $nothing grade

5 grade

[root@biao LinuxTest]#文件名替换和变量[root@biao LinuxTest]# x=*

[root@biao LinuxTest]# echo $x

1.txt~ 4-awk.ppt awkfile awkfile~ ep ep~ grade grade~ nu resultAWK.txt resultShell resultShell~ sub tot tot~

[root@biao LinuxTest]#

[root@biao LinuxTest]# filename=ep    //ep为当前文件夹下的一个文件[root@biao LinuxTest]# mv $filename $filenameX    //报错是因为变量filenameX为空。故语法上的错误。mv:在"ep"后缺少了要操作的目标文件请尝试执行"mv --help"来获取更多信息。[root@biao LinuxTest]# mv $filename ${filename}X   //执行成功。[root@biao LinuxTest]# ls

1.txt~     awkfile   ep~  grade   nu             resultShell   sub  tot~

4-awk.ppt  awkfile~  epX  grade~  resultAWK.txt  resultShell~  tot    //ep文件被替换成了epX

[root@biao LinuxTest]#

[root@biao LinuxTest]# mv epX ep               //先把epX的文件名替换回来。否则找不到文件ep[root@biao LinuxTest]# mv $filename "$filename"X

[root@biao LinuxTest]# ls

1.txt~     awkfile   ep~  grade   nu             resultShell   sub  tot~

4-awk.ppt  awkfile~  epX  grade~  resultAWK.txt  resultShell~  tot

[root@biao LinuxTest]#

$((expression))  expression由变量和运算符构成。$ echo $ ((i+1))   i前面不用$符号。i=$((i*5))

[root@biao LinuxTest]# echo $((i+1))  //i一直都为空。1

[root@biao LinuxTest]# i=$((i*5))

[root@biao LinuxTest]# echo $((i+1))

1编写一个名为nf的程序,显示当前目录中的文件数。键入程序并测试。ls -l | awk '/[^~]$/ {tot+=1} END {print (tot-1)}'[root@biao LinuxTest]# vi nf

[root@biao LinuxTest]# cat nf

ls -l |awk '/[~]/ {tot+=1} END {print (NR-tot-1)}'

[root@biao LinuxTest]# ./nf

10编写一个名为whos的程序,显示排好序的已登录用户清单。只显示用户名,不要有其他信息。键入程序并测试。who获得当前登录系统的所有用户的信息[root@biao LinuxTest]# vi whos

[root@biao LinuxTest]# cat whos

who | awk '{print $1}'

[root@biao LinuxTest]# ./whos

bash: ./whos:权限不够[root@biao LinuxTest]# chmod +x whos

[root@biao LinuxTest]# who

root     tty1         2010-04-03 10:42 (:0)

root     pts/0        2010-04-03 10:45 (:0.0)

abiao    tty7         2010-04-03 15:07 (:1)

[root@biao LinuxTest]# ./whos

root

root

abiao

引用和参数传递

1.[soflib@localhost ~]$ grep google express

google is the best tools for search keyword.

2.[soflib@localhost ~]$ grep google is express

grep: is:没有那个文件或目录shell传递一个参数google给grep在目录找不到is文件夹

express:google is the best tools for search keyword.

3.[soflib@localhost ~]$ grep 'google is' express

google is the best tools for search keyword.shell传递两个参数给grep

shell会忽略掉单引号内的所有特殊字符的原来作用。

4.[soflib@localhost ~]$ text='* means all files in the directory'

[soflib@localhost ~]$ echo $text

1_01.c~ 1.c~ 2.c~ 44.c awkfile awkfile.txt employees.txt ep epp express free的含义.doc grade m m~ m

2 m2~ mfile POSIX.doc qiang something tot UNIX操作系统简介.doc二.c~公共的介绍几个Unix版本.doc介绍几个UNIX的变种.doc模板视频图片文档下载新文件~杨龙linux实验报告.odt音乐桌面means all files in the directory

[soflib@localhost ~]$ echo" $text"

bash: echo * means all files in the directory: command not found

双引号中,除了下面的下面的三种字保留本意外,其他的都被shell忽略了。

1:$

2:反斜杠/

3:反引号``

5.[soflib@localhost ~]$ echo > express把一个空值传到express

[soflib@localhost ~]$ echo />express输出>express/的作用是取消后面字符的具体意义。

>express

[soflib@localhost ~]$ echo $x

[soflib@localhost ~]$ echo /$x

$x

[soflib@localhost ~]$ echo //输出反斜杠

/

6.[soflib@localhost ~]$ lines=one

[soflib@localhost ~]$ >two

[soflib@localhost ~]$ echo "$lines"

one

[soflib@localhost ~]$ echo $lines

one

[soflib@localhost ~]$ echo 'lines'

lines

[soflib@localhost ~]$ lines=one>two

[soflib@localhost ~]$ echo "$lines"

one

[soflib@localhost ~]$ echo $lines

one

[soflib@localhost ~]$ echo $two

[soflib@localhost ~]$ ./two

bash: ./two:权限不够

[soflib@localhost ~]$ chmod

chmod:缺少操作数

请尝试执行"chmod --help"来获取更多信息。

[soflib@localhost ~]$ chmod

chmod:缺少操作数

请尝试执行"chmod --help"来获取更多信息。

[soflib@localhost ~]$ chmod +x two

[soflib@localhost ~]$ ./two

[soflib@localhost ~]$ echo $lines

one

7.[soflib@localhost ~]$ echo your current working directory is `pwd`

your current working directory is /home/soflib

8.[soflib@localhost ~]$ cat two

[soflib@localhost ~]$ now=$(date)

[soflib@localhost ~]$ echo $now

2010

04

13日星期二15:34:45 CST

[soflib@localhost ~]$ filelist=$(ls)

[soflib@localhost ~]$ echo $filelist

1_01.c~ 1.c~ 2.c~ 44.c awkfile awkfile.txt employees.txt ep epp express free的含义.doc grade m m~ m

2 m2~ mfile POSIX.doc qiang something tot two two~ UNIX操作系统简介.doc二.c~公共的介绍几个Unix版本.doc介绍几个UNIX的变种.doc模板视频图片文档下载新文件~杨龙linux实验报告.odt音乐桌面

[soflib@localhost ~]$ namelist=$(cat two)

[soflib@localhost ~]$ echo "$namelist"

echo there are $(who | wc –l )users logged in

10[soflib@localhost ~]$ ((expression))

[soflib@localhost ~]$ expr 1+2

1+2

[soflib@localhost ~]$ i=1

[soflib@localhost ~]$ i=$(expr $i+1)

[soflib@localhost ~]$ expr i

i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值