shell----了解Linux的一些小知识点1

eg1,使用mkdir,一般创建的是目录文件,使用touch,一般创建的是文本文件

区别,使用mkdir ---->  不能使用vim编辑器,因为它是目录文件

          使用touch  ---->可以使用vim编辑器

eg2,使用shell脚本,#! 是一个约定的标记,它告诉系统这个脚本需要什么解释器来执行,即使用哪一种 Shell。

解释器:

$ cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

编写简单的shell文本


[root@localhost test]# /bin/bash test2
Hello World !
[root@localhost test]# cat test2
#!/bin/bash
echo "Hello World !"

使用read关键字,其中read是读取的是输入的数据,需要通过$来进行输出。

[root@localhost test]# /bin/bash test3
What is your name?
wird
Hello, wird
[root@localhost test]# cat test3
#!/bin/bash
echo "What is your name?"
read PERSON
echo "Hello, $PERSON"

可以知道read是读取的是输入的数据,读取到了之后存放在PERSON这个参数里面,如果想要通过echo输出出来,需要通过$加上前面的参数名就能输出出来

eg3,如何检测是否拥有多个进程

知识点;

echo $$    ----->输出当前的进程


[root@localhost test]# /bash test4
-bash: /bash: No such file or directory
[root@localhost test]# /bin/bash test4
4248
[root@localhost test]# echo $$
3343
[root@localhost test]# echo $$
3343
[root@localhost test]# /bin/bash test4
4257
[root@localhost test]# source test4
3343
[root@localhost test]# /bin/bash test4
4266

使用source ,它是强制执行这个脚本,

而使用/bin/bash,会多一个执行的权限

所以就造成了使用/bin/bash执行的进程与直接输入echo $$的进程不一致,而使用source的就和echo $$的进程一致了

eg4,shell的变量

shell支持三种定义的方式,

variable=value variable='value' variable="value"

variable 是变量名,value 是赋给变量的值。如果 value 不包含任何空白符(例如空格、Tab 缩进等),那么可以不使用引号;如果 value 包含了空白符,那么就必须使用引号包围起来。

[root@localhost test]# vim test
test2     test2.sh  test3     test4     test5
[root@localhost test]# vim test5
[root@localhost test]# source test5
http://www.codebaoku.com/shell/
C语言中文网
张三
[root@localhost test]# cat test5
#!/bin/bash
url=http://www.codebaoku.com/shell/
echo $url
name='C语言中文网'
echo $name
author="张三"
echo $author

使用变量


[root@localhost test]# /bin/bash test6
张三
张三
I am good at JavaScript
[root@localhost test]# cat test6
#!/bin/bash
author="张三"
echo $author
echo ${author}
skill="Java"
echo "I am good at ${skill}Script"

修改变量值


[root@localhost test]# vi test7
[root@localhost test]# . test7
http://www.codebaoku.com
http://www.codebaoku.com/shell/
[root@localhost test]# cat test7
#!/bin/bash
url="http://www.codebaoku.com"
echo ${url}
url="http://www.codebaoku.com/shell/"
echo ${url}

采用的是覆盖的思想,

单引号和双引号的区别

[root@localhost test]# . test8
C语言中文网:${url}
C语言中文网:http://www.codebaoku.com
[root@localhost test]# cat test8
#!/bin/bash

url="http://www.codebaoku.com"
website1='C语言中文网:${url}'
website2="C语言中文网:${url}"
echo $website1
echo $website2

 在输出参数上加上 ""(双引号) 之后输出结果则和脚本输出结果一致,即可以换行输出

​ 如果不加 ""(双引号) 则按一行输出

如果变量的内容是数字,那么可以不加引号;如果真的需要原样输出就加单引号;其他没有特别要求的字符串等最好都加上双引号,定义变量时加双引号是最常见的使用场景。

只读变量

使用 readonly 命令可以将变量定义为只读变量,只读变量的值不能被改变。

下面的例子尝试更改只读变量,结果报错:

#!/bin/bash

myUrl="http://www.codebaoku.com/shell/"
readonly myUrl
myUrl="http://www.codebaoku.com/shell/"

会报错

bash: myUrl: This variable is read only.

删除变量

使用 unset 命令可以删除变量。语法:

变量被删除后不能再次使用;unset 命令不能删除只读变量。

[root@localhost test]# . ts

[root@localhost test]# cat ts
#!/bin/bash
myUrl="http://www.codebaoku.com/shell/"
unset myUrl
echo $myUrl
[root@localhost test]# vim ts
[root@localhost test]# . ts
bash: reasonly: command not found...

[root@localhost test]# vim ts
[root@localhost test]# . ts
bash: unset: myUrl: cannot unset: readonly variable
http://www.codebaoku.com/shell/
[root@localhost test]# cat ts
#!/bin/bash
myUrl="http://www.codebaoku.com/shell/"
readonly myUrl
unset myUrl
echo $myUrl

eg5,变量替换

shell变量替换,

Shell 命令替换是指将命令的输出结果赋值给某个变量。

比如,在某个目录中输入 ls 命令可查看当前目录中所有的文件,但如何将输出内容存入某个变量中呢?这就需要使用命令替换了,这也是 Shell 编程中使用非常频繁的功能。

命令替换的算法

例如,date 命令用来获得当前的系统时间,使用命令替换可以将它的结果赋值给一个变量。

Shell 中有两种方式可以完成命令替换,一种是反引号` `,一种是$(),使用方法如下:

variable=`commands` variable=$(commands)


[root@localhost test]# cat ts2
#!/bin/bash

begin_time=`date`    #开始时间,使用``替换
sleep 20s            #休眠20秒
finish_time=$(date)  #结束时间,使用$()替换

echo "Begin time: $begin_time"
echo "Finish time: $finish_time"
[root@localhost test]# /bin/bash ts2
Begin time: Mon Jul  1 02:48:52 EDT 2024
Finish time: Mon Jul  1 02:49:12 EDT 2024

使用 data 命令的%s格式控制符可以得到当前的 UNIX 时间戳,这样就可以直接计算脚本的运行时间了。UNIX 时间戳是指从 1970 年 1 月 1 日 00:00:00 到目前为止的秒数。


[root@localhost test]# vim ts3
[root@localhost test]# . ts3
begin time: 1719817167
finish time: 1719817187
run time: 20s
[root@localhost test]# cat ts3
#!/bin/bash

begin_time=`date +%s`    #开始时间,使用``替换
sleep 20s                #休眠20秒
finish_time=$(date +%s)  #结束时间,使用$()替换
run_time=$((finish_time - begin_time))  #时间差

echo "begin time: $begin_time"
echo "finish time: $finish_time"
echo "run time: ${run_time}s"

第 6 行代码中的(( ))是 Shell 数学计算命令。在 Shell 中进行数据计算不那么方便,必须使用专门的数学计算命令,(( ))就是其中之一。

注意,如果被替换的命令的输出内容包括多行(也即有换行符),或者含有多个连续的空白符,那么在输出变量时应该将变量用双引号包围,否则系统会使用默认的空白符来填充,这会导致换行无效,以及连续的空白符被压缩成一个。请看下面的代码:


[root@localhost test]# . cs1
total 52 -rw-r--r--. 1 root root 147 Jul 1 03:15 cs1 -rwxrwxrwx. 1 root root 33 Jun 28 03:01 test2 -rw-r--r--. 1 root root 33 Jun 28 03:02 test2.sh -rw-r--r--. 1 root root 72 Jun 28 03:25 test3 -rw-r--r--. 1 root root 20 Jun 28 03:54 test4 -rw-r--r--. 1 root root 122 Jun 28 04:13 test5 -rw-r--r--. 1 root root 104 Jun 28 04:18 test6 -rw-r--r--. 1 root root 105 Jun 28 04:21 test7 -rw-r--r--. 1 root root 148 Jun 28 04:30 test8 -rw-r--r--. 1 root root 108 Jun 28 04:51 test9 -rw-r--r--. 1 root root 91 Jul 1 02:40 ts -rw-r--r--. 1 root root 217 Jul 1 02:48 ts2 -rw-r--r--. 1 root root 310 Jul 1 02:59 ts3
--------------------------
total 52
-rw-r--r--. 1 root root 147 Jul  1 03:15 cs1
-rwxrwxrwx. 1 root root  33 Jun 28 03:01 test2
-rw-r--r--. 1 root root  33 Jun 28 03:02 test2.sh
-rw-r--r--. 1 root root  72 Jun 28 03:25 test3
-rw-r--r--. 1 root root  20 Jun 28 03:54 test4
-rw-r--r--. 1 root root 122 Jun 28 04:13 test5
-rw-r--r--. 1 root root 104 Jun 28 04:18 test6
-rw-r--r--. 1 root root 105 Jun 28 04:21 test7
-rw-r--r--. 1 root root 148 Jun 28 04:30 test8
-rw-r--r--. 1 root root 108 Jun 28 04:51 test9
-rw-r--r--. 1 root root  91 Jul  1 02:40 ts
-rw-r--r--. 1 root root 217 Jul  1 02:48 ts2
-rw-r--r--. 1 root root 310 Jul  1 02:59 ts3
[root@localhost test]# cat cs1
#!/bin/bash

LSL=`ls -l`
echo $LSL  #不使用双引号包围
echo "--------------------------"  #输出分隔符
echo "$LSL"  #使用引号包围

 反引号 和 $() 对比

原则上讲,上面提到的两种变量替换的形式是等价的,可以随意使用;但是,反引号毕竟看起来像单引号,有时候会对查看代码造成困扰,而使用 $() 就相对清晰,能有效避免这种混乱。而且有些情况必须使用 $():$() 支持嵌套,反引号不行。

下面的例子演示了使用计算 ls 命令列出的第一个文件的行数,这里使用了两层嵌套。

[c.biancheng.net]$ Fir_File_Lines=$(wc -l $(ls | sed -n '1p')) [c.biancheng.net]$ echo "$Fir_File_Lines" 36 anaconda-ks.cfg

要注意的是,$() 仅在 Bash Shell 中有效,而反引号可在多种 Shell 中使用。所以这两种命令替换的方式各有特点,究竟选用哪种方式全看个人需求。

位置参数

运行 Shell 脚本文件时可以传递参数,这些参数在脚本文件内部可以使用$n的形式来接收,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。我们称之为“位置参数”。

同样,在调用函数时也可以传递参数。Shell 函数参数的传递和其它编程语言不同,没有所谓的形参和实参,在定义函数时也不用指明参数的名字和数目。换句话说,定义 Shell 函数时不能带参数,但是在调用函数时却可以传递参数,这些传递进来的参数,在函数内部就也使用$n的形式接收,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。这种通过$n的形式来接收的参数,在 Shell 中称为“位置参数”。

变量的名字必须以字母或者下划线开头,不能以数字开头;但是位置参数却偏偏是数字,这和变量的命名规则是相悖的,所以我们将它们视为“特殊变量”。


[root@localhost test2]# . test1
Language:
URL:
[root@localhost test2]# cat test1
#!/bin/bash

echo "Language: $1"
echo "URL: $2"
[root@localhost test2]# . test1 shell /http:wwww.baidu.com/
Language: shell
URL: /http:wwww.baidu.com/

给函数传递位置参数‘

[root@localhost test2]# vim test2
[root@localhost test2]# . test2
Language: C++
URL: http://www.codebaoku.com/cplus/
[root@localhost test2]# cat test2
#!/bin/bash

#定义函数
function func(){
    echo "Language: $1"
    echo "URL: $2"
}

#调用函数
func C++ http://www.codebaoku.com/cplus/

特殊变量

Shell脚本 特殊变量:$#、$*、$n、$@、$?、$$

在正常情况下,变量名只能包含数字、字母和下划线。但是,某些 Shell变量 因为包含了其他字符的变量,从而有了特殊含义,这样的变量被称为特殊变量。它们分别是:$#、$*、$n、$@、$?、$$。

变量含义
$0当前脚本的文件名。
$n(n≥1)传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是 $1,第二个参数是 $2。
$#传递给脚本或函数的参数个数。
$*传递给脚本或函数的所有参数。
$@传递给脚本或函数的所有参数。当被双引号" "包含时,$@ 与 $* 稍有不同。
$?上个命令的退出状态,或函数的返回值。
$$当前 Shell 进程 ID。对于 Shell 脚本,就是这些脚本所在的进程 ID。

[root@localhost test2]# cat test2
#!/bin/bash

#定义函数
function func(){
    echo "Language: $1"
    echo "URL: $2"
}

#调用函数
func C++ http://www.codebaoku.com/cplus/
[root@localhost test2]# vim test3
[root@localhost test2]# . test3
Process ID: 3380
File Name: bash
First Parameter :
Second Parameter :
All parameters 1:
All parameters 2:
Total: 0
[root@localhost test2]# . test3 shell yhu
Process ID: 3380
File Name: bash
First Parameter : shell
Second Parameter : yhu
All parameters 1: shell yhu
All parameters 2: shell yhu
Total: 2

[root@localhost test2]# . test4
Language: Java
URL: http://www.codebaoku.com/java/
First Parameter : Java
Second Parameter : http://www.codebaoku.com/java/
All parameters 1: Java http://www.codebaoku.com/java/
All parameters 2: Java http://www.codebaoku.com/java/
Total: 2
[root@localhost test2]# cat test4
#!/bin/bash

#定义函数
function test(){
    echo "Language: $1"
    echo "URL: $2"
    echo "First Parameter : $1"
    echo "Second Parameter : $2"
    echo "All parameters 1: $@"
    echo "All parameters 2: $*"
    echo "Total: $#"
}

#调用函数
test Java http://www.codebaoku.com/java/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值