设为首页 收藏本站
查看: 811|回复: 0

[经验分享] linux学习命令总结⑨

[复制链接]

尚未签到

发表于 2018-5-24 08:10:48 | 显示全部楼层 |阅读模式
  #bash中的变量
  本地变量:
  定义变量:[set]Var_Name="Value"
  引用变量:¥{Var_name}
  撤销变量:unset Var_name
[root@VM_168_102_centos ~]# num=5
[root@VM_168_102_centos
~]# echo $num
5
[root@VM_168_102_centos
~]# unset num
[root@VM_168_102_centos
~]# echo $num

  解释说明:只对当前shell进程有效,对其子进程及其他shell进程均无效

[root@VM_168_102_centos ~]# wanghan=openstack
[root@VM_168_102_centos
~]# echo $wanghan
openstack
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos
/root]$ echo $wanghan
wanghan: Undefined variable.
[wanghan@VM_168_102_centos
/root]$


[Var_name@VM_168_102_centos rott]$ su Var_name
Password:
[Var_name@VM_168_102_centos rott]$ exit
exit
[root@VM_168_102_centos
~]# su Var_name
[Var_name@VM_168_102_centos rott]$ sum
=88
[Var_name@VM_168_102_centos rott]$ echo $sum
88
[Var_name@VM_168_102_centos rott]$ exit
exit
[root@VM_168_102_centos
~]# echo $sum
[root@VM_168_102_centos
~]# su Var_name
[Var_name@VM_168_102_centos rott]$ echo $sum
[Var_name@VM_168_102_centos rott]$


  显示所有本地shell变量:
  输入set命令查看本地所有shell变量
  局部变量:局部变量就是在局部起作用的变量,用local内建命令定义
  环境变量:对当shell进程及其子shell有,但用户注销时这些值将丢失

[root@VM_168_102_centos ~]# sum=88
[root@VM_168_102_centos
~]# export sum
[root@VM_168_102_centos
~]# echo $sum
88
[root@VM_168_102_centos
~]# su wanghan
[wanghan@VM_168_102_centos
/root]$ echo $sum
88
[wanghan@VM_168_102_centos
/root]$ exit
exit
[root@VM_168_102_centos
~]# echo $sum
88
[root@VM_168_102_centos
~]#


[root@VM_168_102_centos ~]# su Var_name
[Var_name@VM_168_102_centos rott]$ test
=hello
[Var_name@VM_168_102_centos rott]$ export test
[Var_name@VM_168_102_centos rott]$ echo $test
hello
[Var_name@VM_168_102_centos rott]$ exit
exit
[root@VM_168_102_centos
~]# echo $test
[root@VM_168_102_centos
~]# su Var_name
[Var_name@VM_168_102_centos rott]$ echo $test
[Var_name@VM_168_102_centos rott]$


  位置变量:

#!/bin/bash
echo $
1
echo $
2
echo $
0



[root@VM_168_102_centos ~]# bash lianxi_3_1.sh 2 4
2    //对应 $1
4   //对应 $2
lianxi_3_1.sh  //对应$0
//$0取自身文件名


  特殊变量:shell对一些参数做特殊处理,这些参数只能被引用而不能被赋值

#!/bin/bash
echo $
0  
echo $
1
echo $
2
echo $
3
echo $
4
echo $
5
echo $#   
//传递到脚本的参数个数;
echo $*   // 以一个单字符串显示所有向脚本传递的参数。与位置变量不同,此选项参数可超过9个;
echo $$  //脚本运行的当前进程ID号
echo $!  //后台运行的最后一个进程的进程ID号
echo $@ //与$*相同,但是使用时加引号,并在引号中返回每个参数
echo $-   //显示shell使用的当前选项,与set命令功能相同
echo $?  // 显示最后命令的退出状态.0表示没有错误,其他任何值表明有错误;


[root@VM_168_102_centos ~]# bash lianxi_3_1.sh 1 2 3 4 5 6
lianxi_3_1.sh
1
2
3
4
5
6
1 2 3 4 5 6
25120
1 2 3 4 5 6
hB
0

  #for循环:
        for var_Name in 列表; do
            语句1
            语句2
            ...
        done
  写一个脚本:添加10个用户,user101-user110:

#!/bin/bash
for i in {101..110}; do
useradd user$i
echo
"useradd user$i"
done


  bash –n 脚本文件 检查语法错误(变量等输入错误不提示,无错误无显示)

[root@VM_168_102_centos ~]# bash -n for_test1.sh
for_test1.sh: line
7: syntax error: unexpected end of file
[root@VM_168_102_centos
~]#

  bash –x 显示脚本运行的每一步骤(帮助分析脚本)

[root@VM_168_102_centos ~]# bash -x for_test1.sh
+ for i in '{101..110}'
+ useradd user101
useradd: user
'user101' already exists
+ echo 'useradd user101'
useradd user101
+ for i in '{101..110}'
+ useradd user102
useradd: user
'user102' already exists
+ echo 'useradd user102'
useradd user102
+ for i in '{101..110}'
+ useradd user103
useradd: user
'user103' already exists
+ echo 'useradd user103'
useradd user103
+ for i in '{101..110}'
+ useradd user104
useradd: user
'user104' already exists
+ echo 'useradd user104'
useradd user104
+ for i in '{101..110}'
+ useradd user105
useradd: user
'user105' already exists
+ echo 'useradd user105'
useradd user105
+ for i in '{101..110}'
+ useradd user106
useradd: user
'user106' already exists
+ echo 'useradd user106'
useradd user106
+ for i in '{101..110}'
+ useradd user107
useradd: user
'user107' already exists
+ echo 'useradd user107'
useradd user107
+ for i in '{101..110}'
+ useradd user108
useradd: user
'user108' already exists
+ echo 'useradd user108'
useradd user108
+ for i in '{101..110}'
+ useradd user109
useradd: user
'user109' already exists
+ echo 'useradd user109'
useradd user109
+ for i in '{101..110}'
+ useradd user110
useradd: user
'user110' already exists
+ echo 'useradd user110'
useradd user110


  #wc命令:统计指定文件的字数、字节数、行数,并输出显示

[root@VM_168_102_centos ~]# wc /etc/passwd
57   68 2507 /etc/passwd

  wc –l:统计行数

[root@VM_168_102_centos ~]# wc -l /etc/passwd
57 /etc/passwd

  wc –c:统计字节数量

[root@VM_168_102_centos ~]# wc -c /etc/passwd
2507 /etc/passwd

  wc –w:统计单词数量

[root@VM_168_102_centos ~]# wc -w /etc/passwd
68 /etc/passwd

  tr命令:转换字符或删除字符
  讲所有小写替换成大写

[root@VM_168_102_centos ~]# tr [a-z] [A-Z]
as
AS



[root@VM_168_102_centos ~]# echo sccccc | tr "c" "C"
sCCCCC


  tr –d:删除指定字符

[root@VM_168_102_centos ~]# echo sccabc | tr -d "c"
sab


  #cut命令:选取命令,就是将一段数据经过分析,取出我们想要的。(按行分析)
  cut -d:自定义分隔符
  cut -f:与-d一起使用,指定显示哪个区域
  单个数字:一个字段

[root@VM_168_102_centos ~]# tail /etc/passwd | cut -d: -f1
user103
user104
user105
user106
user107
user108
user109
user110
testuser1


  
逗号分隔的多个数字:指定多个离散字段

[root@VM_168_102_centos ~]# tail /etc/passwd | cut -d: -f1,3
user103:
3005
user104:
3006
user105:
3007
user106:
3008
user107:
3009
user108:
3010
user109:
3011
user110:
3012
testuser1:
3013
testuser2:
3014


-:连续字段,如3-5
[root@VM_168_102_centos ~]# tail /etc/passwd | cut -d: -f1-4
user103:x:
3005:3005
user104:x:
3006:3006
user105:x:
3007:3007
user106:x:
3008:3008
user107:x:
3009:3009
user108:x:
3010:3010
user109:x:
3011:3011
user110:x:
3012:3012
testuser1:x:
3013:3013
testuser2:x:
3014:3014

  cut -c:以字符为单位进行分割

[root@VM_168_102_centos ~]# tail /etc/passwd | cut -c 2     
s
s
s
s
s
s
s
s
e
e
[root@VM_168_102_centos
~]# tail /etc/passwd | cut -c 2-4
ser
ser
ser
ser
ser
ser
ser
ser
est
est
[root@VM_168_102_centos
~]# tail /etc/passwd | cut -c 2,5
s1
s1
s1
s1
s1
s1
s1
s1
eu
eu


  #sort命令:依据不同的数据类型进行排序

[root@VM_168_102_centos ~]# cat sort.sh
q
s
w
d
f
v
A
R
T
1
4
6
78
2
[root@VM_168_102_centos
~]# sort sort.sh
1
2
4
6
78
A
R
T
d
f
q
s
v
w


  sort -f:排序时,忽略字符大小写

[root@VM_168_102_centos ~]# sort sort.sh
1
2
4
6
78
A
R
T
d
f
q
s
v
w
[root@VM_168_102_centos
~]# sort -f sort.sh
1
2
4
6
78
A
d
f
q
R
s
T
v
w


  sort -f:按数值大小进行排序

[root@VM_168_102_centos ~]# sort -n sort.sh
A
R
T
d
f
q
s
v
w
1
2
4
6
78

  #uniq命令:移除连续重复的行

[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos
~]# uniq uniq.sh
a
b
f
b
c
e


  uniq -c:显示每行重复的次数

[root@VM_168_102_centos ~]# uniq -c uniq.sh
3 a
1 b
1 f
2 b
1 c
1 e


uniq -d:仅显示连续重复的行
[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos
~]# uniq -d uniq.sh
a
b


  uniq -u:显示不连续重复的行

[root@VM_168_102_centos ~]# cat uniq.sh
a
a
a
b
f
b
b
c
e
[root@VM_168_102_centos
~]# uniq -u uniq.sh
b
f
c
e

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-480466-1-1.html 上篇帖子: 初用linux 下篇帖子: Linux的I/O调度
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表