Shell

本文详细介绍了Shell脚本的基础知识,包括bash执行顺序、注释、变量导出、随机数生成、UUID和MAC地址生成。讲解了变量、特殊变量的使用,以及echo、exec、read、shift等命令。此外,还涵盖了条件测试、循环结构、函数定义、数组操作等内容,是学习Shell编程的实用教程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

查看bash是否安全:

[root@localhost ~]# env x='(){:;}; echo be careful' bash -c " echo this is a test"
this is a test
*bash执行顺序:

/etc/profile–>/etc/profile.d–>~/.bash_profile–>/.bashrc–>/etc/bashrc

[root@localhost ~]# /root/i.sh
-bash: /root/i.sh: Permission denied
[root@localhost ~]# chmod +x i.sh
[root@localhost ~]# ./i.sh
HELLO world
执行脚本方法:
[root@localhost ~]# /root/i.sh
HELLO world
[root@localhost ~]# sh i.sh
HELLO world
[root@localhost ~]# source i.sh
HELLO world
[root@localhost ~]# . i.sh
HELLO world
[root@localhost ~]# bash i.sh
HELLO world

.和source加载文件,将局部变量加载成全局变量

多行注释:

:<<BLOCK

代码…

BLOCK

没有必要使用cat命令

eg:cat /etc/passwd | grep guru

使用1 guru /etc/passwd即可

变量的导出:

set:输出所有变量

env:输出全局变量

declare:输出所有变量、函数和已经导出的变量

定义全局变量:

[root@localhost ~]# export NAME=anliu
[root@localhost ~]# env |grep NAME
HOSTNAME=localhost.localdomain
NAME=anliu
LOGNAME=root
[root@localhost ~]# AGE=18
[root@localhost ~]# env |grep AGE
[root@localhost ~]# export AGE
[root@localhost ~]# env |grep AGE
AGE=18

设置登录提示语句:

[root@localhost ~]# vi /etc/motd

生成加密码:

CentOS 6:

grub-mkpasswd-pbkdf2

CentOS 7:

grub2-mkpasswd-pbkdf2

生成自然数:

[root@localhost ~]# echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# echo {00..10}
00 01 02 03 04 05 06 07 08 09 10

[root@localhost ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# seq 10 2 20(10~20,步长为2)
10
12
14
16
18
20
[root@localhost ~]# seq 8 10
8
9
10

-w:与尾数宽度相等

[root@localhost ~]# seq -w 8 10
08
09
10
[root@localhost ~]# seq -w 1 03
01
02
03

-f:格式化输出

[root@localhost ~]# seq -f "%3g" 8 12
  8
  9
 10
 11
 12
[root@localhost ~]# seq -f "%03g" 8 12
008
009
010
011
012

-s:使用指定字符串分隔数字

[root@localhost ~]# seq -s "`echo -e "\t"`" 8 12
8       9       10      11      12
[root@localhost ~]# a="北京欢迎你:\n 世界~~"
[root@localhost ~]# echo $a
北京欢迎你:\n 世界~~
[root@localhost ~]# echo -e $a
北京欢迎你:
 世界~~
生成随机数:
1、[root@localhost ~]# echo $RANDOM

2、[root@localhost ~]# awk 'BEGIN{srand();print rand()}'(生成随机小数)
0.681664

3、[root@localhost ~]# openssl rand -base64 12|md5sum |cut -c 1-8(截取8位随机数,带字母,md5sum加密)
d1df9d56

4、[root@localhost ~]# date +%s%N |cut -c 8-15
13164971
[root@localhost ~]# date +%s%N |md5sum |head -c 8(加密生成)
283ce550
5、[root@localhost ~]# cat /dev/urandom |head -n 10 |md5sum  |head -c 10
bf093e0b08
生成随机UUID:
[root@localhost ~]# cat /proc/sys/kernel/random/uuid
eed65d5c-95f8-4b02-833d-db03db002356
生成随机MAC地址:
[root@localhost ~]# echo "00:60:2F$(dd bs=1 count=3 if=/dev/random 2>/dev/null |hexdump -v -e ' /1 ":%02X"')"
00:60:2F:DD:E6:B2
[root@localhost ~]# [ -f file1 ] || touch file1(判断是否有file1,||:若没有,则创建file1)
[root@localhost ~]# [ -f file2 ] || echo "file2 meiyou"
file2 meiyou
[root@localhost ~]# [ -f file1 ] && echo "file1 you"(&&:若成立)
file1 you
变量:

变量是计算机内存的单元,其中存放的值可以改变;当shell脚本需要保存一些信息时,如一个文件名或是一个数字,就把它放到一个变量中。

**命名规则:**可以由字母、数字和下划线组成,但是不能以数字开头,如果变量名是“2name”则是错误的。

在Bash中,变量的默认类型都是字符串型,如果要进行数值运算,则必须指定变量类型为数值型。

1、变量用等号连接值,等号左右不能有空格。

2、变量的值如果有空格,需要使用单引号或双引号包括。

3、在变量的值中,可以使用“\”转义符。

4、如果需要增加变量的值,那么可以进行变量值的叠加。不过变量需要用双引号包含“ 变 量 名 ” 或 用 变量名”或用 {变量名}包含。

5、如果是把命令的结果作为变量值赋予变量,则需要使用反引号或$()包含命令。

6、环境变量名建议大写,便于区分。

调用变量:echo $变量名

删除变量:unset 变量名

变量叠加:

[root@web01 ~]# aa=123
[root@web01 ~]# echo $aa
123
[root@web01 ~]# aa="$aa"456(或者为aa=${aa}456)
[root@web01 ~]# echo $aa
123456

查看系统中所有变量:set

特殊变量:

$0:输出脚本名

$n:获取当前执行的shell脚本的第n个参数值

$#:判断参数个数

$ *:获取当前shell脚本所有传参的参数,不加引号和$@相同,加引号把传递的参数当做整体,相当于“$1 $2 $3”

$ @:获取当前shell脚本所有传参的参数,不加引号和$*相同,加了双引号是所有参数视为不同的独立字符串,相当于“$1”“$2”“$3”

$$:获取当前执行的shell脚本的进程号(PID),这个变量不常用

$!:获取上一个在后台工作的进程的进程号(PID),不常用

$?:1、判断命令、脚本是否执行成功

2、若在脚本中调用执行“exit数字”,则会返回这个数字给“$?”变量

3、如果是在函数里,则通过“return数字”把这个数字以函数返回值的形式传给“$?”

[root@localhost ~]# dirname /root/4.test.sh
/root
[root@localhost ~]# basename(当前路径)
/root/4.test.sh
4.test.sh
echo:

打印输出

-e:解析转义字符

-n:不换行输出

[root@localhost ~]# echo "123";echo "456"
123
456
[root@localhost ~]# echo -n  "123";echo -n  "456"
123456[root@localhost ~]# echo -n  "123";echo "456"
123456
exec:

在不创建新的子进程的前提下,转去执行指定的命令,当指定的命令执行完毕后,该进程也就终止

[root@localhost ~]# su user01
[user01@localhost root]$ exec date
Sun Sep  8 19:39:11 CST 2019
[root@localhost ~]# 
## 执行完成后,返回root

当用exec打开文件后,read命令每次都会将文件指针移动到文件的下一行进行读取,直到文件末尾,利用这个可以实现处理文件内容

[root@localhost ~]# cat 9.test.sh
cat  /tmp/test.txt
while read line
do
        echo $line
done
[root@localhost ~]# sh 9.test.sh
1
2
3
4
5
6
7
8
9
10




^C
## 当用cat时,进程不终止


[root@localhost ~]# cat 9.test.sh
exec <  /tmp/test.txt
while read line
do
        echo $line
done
[root@localhost ~]# sh 9.test.sh
1
2
3
4
5
6
7
8
9
10
## 当用exec时,执行完成直接退出
read:

从标准输入中读取字符串信息,传给shell内部定义的变量

-n:用于限定最多可以有多少字符可以作为有效读入

-p:设置提示信息,即在输入前打印信息

[root@localhost ~]# cat 1.test.sh
read " 请输入数字:" num
echo $num
[root@localhost ~]# sh 1.test.sh
q1
1.test.sh: line 1: read: ` 请输入数字:': not a valid identifier


[root@localhost ~]# cat 1.test.sh
read -p  " 请输入数字:" num
echo $num
[root@localhost ~]# sh 1.test.sh
 请输入数字:1
1
shift:

重新命名所有的位置参数;每使用一次shift,所有位置参数依次向左移一位,并且位置参数$#会减1

[root@localhost ~]# cat 2.test.sh
echo "$1 $2 $3 $4 $5"
[root@localhost ~]# sh 2.test.sh 1 2 3 4 5
1 2 3 4 5


[root@localhost ~]# cat 2.test.sh
echo "$1 $2 $3 $4 $5"
echo $3
[root@localhost ~]# sh 2.test.sh 1 2 3 4 5
1 2 3 4 5
3


[root@localhost ~]# cat 2.test.sh
echo "$1 $2 $3 $4 $5"
shift
echo $3
[root@localhost ~]# sh 2.test.sh 1 2 3 4 5
1 2 3 4 5
4
统计字符串长度、截取字符串
[root@localhost ~]# a="we love linux,linux makes us happy"
[root@localhost ~]# echo ${a}
we love linux,linux makes us happy
[root@localhost ~]# echo ${#a}(统计字符串长度)
34
[root@localhost ~]# expr length "$a"
34
[root@localhost ~]# echo $a |wc -L
34
[root@localhost ~]# echo "$a" |awk '{print length($0)}'
34
[root@localhost ~]# b="abcdefg"
[root@localhost ~]# echo ${b:2}(提取子串,从第二个字符之后到结尾)
cdefg
[root@localhost ~]# echo ${b:2:4}(提取子串,从第二位以后开始,截取四位)
cdef
[root@localhost ~]# echo ${b#c}
abcdefg
[root@localhost ~]# echo ${b#a*c}
defg
[root@localhost ~]# b="abcdefgabcdbcde"
[root@localhost ~]# echo ${b#a*c}(一个#是最短匹配,删除最近从a到c的字符串)
defgabcdbcde
[root@localhost ~]# echo ${b##a*c}(两个#是最长匹配,删除以a开头以最后为c结尾的字符串)
de
[root@localhost ~]# echo ${b%c*e}(从变量$结尾开始删除最短匹配的word子串)
abcdefgabcdb
[root@localhost ~]# echo $b
abcdefgabcdbcde
[root@localhost ~]# echo ${b%%c*e}(从变量$结尾开始删除最长匹配的word子串)
ab
批量修改文件名
[root@localhost test1]# for i in {01..20};do echo "123" > file_test_abc_$i.txt;done(生成文件file_test_abc_01.txt~file_test_123_20.txt)
创建脚本文件编辑:
for i in `ls /root/test1`
do
echo $i
f="$i"
echo ${f/abc/123}
mv /root/test1/$i /root/test1/${f/abc/123}
done
~
将所有文件名中abc改为123

取消变量:[root@localhost test1]# unset a

+、-、=、?
[root@localhost test1]# a=mysql
[root@localhost test1]# echo $a
mysql
[root@localhost test1]# echo ${a:+linux}(覆盖,非永久)
linux
[root@localhost test1]# echo $a
mysql
[root@localhost test1]# a=mysql
[root@localhost test1]# echo $a
mysql
[root@localhost test1]# echo ${a:-linux}(当a有值时,输出原a的值)
mysql
[root@localhost test1]# unset a
[root@localhost test1]# echo ${a:-linux}(当a没有值时,输出-后的值)
linux
[root@localhost test1]# unset a
[root@localhost test1]# echo ${a:=linux}(当a为空时,为a赋值)
linux
[root@localhost test1]# echo $a
linux
[root@localhost test1]# echo ${a:=mysql}(当a有值时,不做替换)
linux
[root@localhost test1]# b=mysql
[root@localhost test1]# echo ${b:?linux}(如果变量值为空或未赋值,那字符串将被作为标准错误输出,否则输出变量的值)
mysql
[root@localhost test1]# echo $b
mysql
Shell中常见的算术运算命令:

(()):用于整数运算的常用运算符

let:用于整数运算,类似于(())

expr:可用于整数运算,还有额外功能

bc:Linux的下一个计算器程序

$[]:用于整数运算

awk:既可以用于整数运算,又可以用于小数运算

declare:定义变量值和属性,-i参数可以用于定义整型变量,做运算

(()):
[root@localhost test1]# ((a=1+2**3-4%3))
[root@localhost test1]# echo $a
8
[root@localhost test1]# a=$((a=1+2**3-4%3))
[root@localhost test1]# echo $a
8
[root@localhost test1]# echo $((3>8))(错误输出0)
0
[root@localhost test1]# echo $((3<8))(正确输出1)
1

脚本文件中编辑:

read -p "a:" a
read -p "b:" b
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"

执行脚本文件:

[root@localhost test1]# sh 10.txt.sh
a:5
b:3
a-b=2
a+b=8
a*b=15
a/b=1
let:
[root@localhost test1]# let a=1+2
[root@localhost test1]# echo $a
3
expr:

中间必须有空格,乘法需要转义符\

[root@localhost test1]# expr 1 + 1
2
[root@localhost test1]# expr 1 \* 2
2
[root@localhost test1]# expr 10 \* 2
20
[root@localhost test1]# expr 10 \/ 2
5

编辑脚本文件:

read -p "a:" a
expr $a + 1 >/dev/null 2>&1
if [ $? -ne 0 ]
then
        echo "请输入整数.."
        exit 3
fi

read -p "b:" b
expr $b + 1 >/dev/null 2>&1
if [ $? -ne 0 ]
then
    echo "请输入整数.."
     exit 3
fi
echo "a-b=$(($a-$b))"
echo "a+b=$(($a+$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"

执行脚本文件:

[root@localhost test1]# sh 10.txt.sh
a:1
b:2
a-b=-1
a+b=3
a*b=2
a/b=0
Shell脚本之条件测试

1、

[root@localhost ~]# [ -d test1 ] && echo ok || echo bu ok
ok
[root@localhost ~]# [ -d test7 ] && echo ok || echo bu ok
bu ok

在这里插入图片描述

常用文件测试操作符:

**-d:**判断该文件是否存在,并且是否为目录文件(是目录为真)

**-f:**文件存在且为普通文件则为真,即测试表达式成立

-e:文件存在则为真,不辨认是目录还是文件

-r:文件存在且可读则为真

-w:文件存在且可写则为真

-x:文件存在且可执行则为真

-s:文件存在且文件大小不为0则为真

-L:文件存在且为链接文件则为真

[root@localhost ~]# [ -L test1 ] && echo ok || echo bu ok
bu ok

file1 -nt file2:文件f1比文件f2新则为真

file1 -ot file2:文件f1比文件f2旧则为真

file1 -ef file2:文件file1和文件file2的Inode号相同则为真(可理解为两个文件是否为同一个文件)可用于判断软硬连接

字符串测试表达式:

-n “字符串”:判断字符串是否为非空(为非空返回真)

-z “字符串”:判断字符串是否为空(为空返回真)

“字符串1”=“字符串”:若字符串1等于字符串2,则为真

“字符串1”!=“字符串”:若字符串1不等于字符串2,则为真

字符串必须带双引号

[root@web01 ~]# name=sc
[root@web01 ~]# [ -z "$name" ] && echo yes || echo no
no
整数比较操作:
在[]以及test中使用的比较符号在(())和[[]]中使用的比较符号说明
-eq==或=相等
-ne!=不相等
-gt>大于
-ge>=大于等于
-lt<小于
-le<=小于等于

比较符号两端也要有空格

[root@web01 ~]# [ 23 -gt 23 ] && echo yes || echo no
no
逻辑操作符:
在[]和test中使用的操作符在[[]]和(())中使用的操作说明
-a&&与,两端都为真,则结果为真
-o||或,两端有一个为真,则结果为真
!!非,两端相反,则结果为真

编辑脚本文件:

i=0
while ((i<7))
do
    echo $i
    let i++
done

执行该脚本文件:

[root@localhost ~]# sh 14.txt.sh
0
1
2
3
4
5
6
if条件语句
单分支
if [ 条件判断式 ];then
​  程序
if  

或者

if [ 条件判断式 ]
then
​  程序
if  
#!/bin/bash(脚本中必须有)
rate=$(df -h |grep /dev/sda1 |awk '{print $5}' |cut -d "%" -f1)
if [ $rate -ge 50 ];then
echo "warning!"
 fi
双分支
if [ 条件判断式1 ]
then
  条件成立时,执行的程序
else
  条件不成立时,执行的另一个程序
fi
case语句
case $变量名 in
"值1")
如果变量的值等于值1,则执行程序1
;;
"值2")
如果变量的值等于值2,则执行程序2
;;
.....省略其他分支
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
[root@nfs01 ~]# vim 2.sh
#!/bin/bash
echo "you want to go to shanghai,please input '1'"
echo "you want to go to guangzhou,please input '2'"
echo "you want to go to beijing,please input '3'"
read -t 30 -p "please input your num: " cho
case "$cho" in
"1")
   echo "you will going to shanghai"
;;
"2")
   echo "you will going to  guangzhou"
;;
"3")
   echo "you will going to beijing"
;;
*)
  echo "please input the right num"
;;
esac

在这里插入图片描述

编写一个9*9乘法表:

vi 3.sh

i=1
while ((i<=9))
do
        j=1
        while ((j<=i))
        do
let num=$i*$j
echo -en "$i*$j=$num\t"
               let j++
        done
echo ""
let i++
done
~

for循环
语法一
for 变量 in 值1 值2 值3(只要有空格,就默认为一个值)
do
   程序
done
[root@nfs01 ~]# vim 3.sh
#!/bin/bash
for i in morning aftonoon night
do
   echo "this time is $i"
done
语法二
for (( 初始值;循环控制条件;变量变化 ))
do
   程序
done
#!/bin/bash
#从1加到100
s=0
for (( i=1;i<=100;i=i+1 ))
do
 s=$(( $s+$i ))
done
echo $s
脚本运行语法:

在这里插入图片描述

定义函数:
function test(){
echo "this is a function test"
}
test

function test(){
echo "this is a function test"
}

test1(){
echo "this is a test1"
}
test1
test
[root@localhost ~]# vim 5.sh
function test(){
echo "this is a function test"
}

test1(){
echo "this is a test1"
}

function test2
{
echo "this is test2"
}

test1
test
test2
ping(){
for i in `seq $1 $2`
do
echo $i
ping -c 1 192.168.42.$i >/dev/null 2>&1
echo $?
if [ $? -eq 0 ]
then
echo "$i tong.."
else
echo "$i butong.."
fi
done
return 1
}
ping 180 185

数组:

[root@localhost test3]# ls
1.test.sh  8.test.sh  9.test.sh

[root@localhost test3]# array2=(`ls`)

[root@localhost test3]# for i in ${array2[@]};do echo $i;done
1.test.sh
8.test.sh
9.test.sh
[root@localhost ~]# array2=(12 23 "we love linux,linux makes us sad..")
[root@localhost ~]# for i in ${array2[@]};do echo $i;done
12
23
we
love
linux,linux
makes
us
sad..
数组
定义数组:

方法一:
一次赋一个值
组名[下标]=变量值

[root@localhost test3]# array[0]=runrun
[root@localhost test3]# array[1]=yuyu
[root@localhost test3]# array[2]=honghong

[root@localhost test3]# for i in ${array[@]};do echo $i;done
runrun
yuyu
honghong

方法二:
一次赋多个值

[root@localhost test3]# array1=(yuanyuan xiaobao yangyang
[root@localhost test3]# for i in ${array1[@]};do echo $i;done
yuanyuan
xiaobao
yangyang

将文件名放到数组中:

[root@localhost test3]# ls
1.test.sh  8.test.sh  9.test.sh
[root@localhost test3]# array2=(`ls`)
[root@localhost test3]# for i in ${array2[@]};do echo $i;done
1.test.sh
8.test.sh
9.test.sh

数组中传数字,字符串:
[root@localhost test3]# array3=(12 23 "we love linux,linux make happy...")

[root@localhost test3]# for i in ${array3[@]};do echo $i;done
12
23
we
love
linux,linux
make
happy...
//逢空格就换行作业:(将文件内容输出,文件有空格,)

[root@localhost test3]# array4=(12 23 [33]="123")
[root@localhost test3]# for i in ${array4[@]};do echo $i;done
12
23
123
[root@localhost test3]# for i in ${array4[33]};do echo $i;done
123 

[]:表示指定下标,即指定数组的各个部分

[root@localhost test3]# array[0]=111
[root@localhost test3]# array[1]=222
[root@localhost test3]# array[2]=333
[root@localhost test3]# array[3]=444
[root@localhost test3]# for i in ${array[@]};do echo $i;done
111
222
333
444
获取数组元素的个数

#:取得数组元素的个数

[root@localhost test3]# length=${#array[@]}
[root@localhost test3]# echo $length
4

或者使用

[root@localhost test3]# length=${#array[*]}
[root@localhost test3]# echo $length
4

[root@localhost test3]# length=${#array[1]}
[root@localhost test3]# echo $length
3
取某一元素的长度

删除:

unset array ##删除整个数组
unset array[1] ##删除某个元素

查看
[root@localhost test3]# declare -a
[root@localhost test3]# echo ${array1[0]}
aaa

6)修改数组
declare -a array1='([0]=aaa)'
##将array1的

7)打印索引值

重置数组

declare -a array1='([0]=aaa)'
##将array1更改为aaa

[root@localhost ~]# array1=(123 456 789)
[root@localhost ~]# for i in ${array1[@]};do echo $i;done
123
456
789
[root@localhost ~]# declare -a array1='([0]=999)'
[root@localhost ~]# for i in ${array1[@]};do echo $i;done
999
打印索引值
[root@localhost test3]# echo ${!array2[@]}
0 1 2
[root@localhost test3]# echo ${!array3[@]:1}
sh参数:

sh -x:查看脚本运行过程,在打印过程中,也执行文件

sh -n:检查脚本是否存在语法错误,不运行脚本

sh -v:先输出脚本内容,然后执行脚本,如果有错误,会给出错误提示

i=0
n=5
while [ "$i" -lt $n ]
do
echo $i
   let i++
read array[$i]
b=${array[$i]}
echo "$b"
done
for i in ${array[@]};do echo $i;done

将字符一个一个输出

KaTeX parse error: Expected '}', got 'EOF' at end of input: {chars:i:1}:表示从chars字符串的$i位置开始,一次获取一个字符

chars='linux'
for ((i=0;i<${#chars};i++))
do
array[$i]=${chars:$i:1}
echo ${array[$i]}
done

将数组array2中的每一个元素输出时*8

array2=(`seq 3`)
for i in ${array2[@]}
do
let i=i*8
echo $i
done
一行一行读出文件/etc/passwd内容:

方法一:

while read line
do
echo ------------
echo $line
done </etc/passwd

方法二:

IFS_old=$IFS
IFS=$"\n"
array5=(`cat /etc/passwd`)
for i in ${array5[@]}
do
echo $i
done
IFS=$IFS_old

输出/etc/passwd内容:

file_scan(){
array5=(`cat /etc/passwd`)
for i in ${array5[@]}
do
   echo $i
done
}
vir(){
  IFS_old=$IFS
  IFS=$'\n'
  $1
  IFS=$IFS_old
}
vir file_scan
scat_passwd(){
for i in `cat /etc/passwd`
do
   echo $i
done
}

scat_test(){
for k in `cat ./test.txt`
do
echo $k
done
}

scat_selinux(){
for j in `cat /etc/sysconfig/selinux`
do echo $j
done
}

vir(){
IFS_old=$IFS
IFS=$'\n'
$1
IFS=$IFS_old
}
vir scat_passwd
scat_test
vir scat_selinux

n:检查脚本是否存在语法错误,不运行脚本

sh -v:先输出脚本内容,然后执行脚本,如果有错误,会给出错误提示

i=0
n=5
while [ "$i" -lt $n ]
do
echo $i
   let i++
read array[$i]
b=${array[$i]}
echo "$b"
done
for i in ${array[@]};do echo $i;done

将字符一个一个输出

KaTeX parse error: Expected '}', got 'EOF' at end of input: {chars:i:1}:表示从chars字符串的$i位置开始,一次获取一个字符

chars='linux'
for ((i=0;i<${#chars};i++))
do
array[$i]=${chars:$i:1}
echo ${array[$i]}
done

将数组array2中的每一个元素输出时*8

array2=(`seq 3`)
for i in ${array2[@]}
do
let i=i*8
echo $i
done
一行一行读出文件/etc/passwd内容:

方法一:

while read line
do
echo ------------
echo $line
done </etc/passwd

方法二:

IFS_old=$IFS
IFS=$"\n"
array5=(`cat /etc/passwd`)
for i in ${array5[@]}
do
echo $i
done
IFS=$IFS_old

输出/etc/passwd内容:

file_scan(){
array5=(`cat /etc/passwd`)
for i in ${array5[@]}
do
   echo $i
done
}
vir(){
  IFS_old=$IFS
  IFS=$'\n'
  $1
  IFS=$IFS_old
}
vir file_scan
scat_passwd(){
for i in `cat /etc/passwd`
do
   echo $i
done
}

scat_test(){
for k in `cat ./test.txt`
do
echo $k
done
}

scat_selinux(){
for j in `cat /etc/sysconfig/selinux`
do echo $j
done
}

vir(){
IFS_old=$IFS
IFS=$'\n'
$1
IFS=$IFS_old
}
vir scat_passwd
scat_test
vir scat_selinux
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值