一组Linux Shell Scripting小练习

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dgd2010.blog.51cto.com/1539422/1718284

# Linux shell将字符串分割成数组

1
2
result=$(facter |  awk  '/ipaddress/ && !/ipaddress_lo/ {print $1 " " $3}' )
array=($result)

# 判断一个变量是否存在(不是判断是否为空)

1
if  [ -z ${var+x} ];  then  echo  "var is unset" else  echo  "var is set to '$var'" fi

# 判断一个变量是否为空

1
2
if  "$var x"  " x"  ];  then  echo  "var is empty" else  echo  "var is set to '$var'" fi
if  [ -z $var ];  then  echo  "var is empty" else  echo  "var is set to '$var'" fi

#系统变量用后还原

# 关于IFS的定义:IFS,Internal Field Separator

# An Internal Field Separator (IFS) is an environment variable that stores delimiting characters.

# It is the default delimiter string used by a running shell environment.

# "$*" expands as "$1c$2c$3", where c is the first character of IFS

# When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.

# That is, "$*" is equivalent to "$1c$2c...", where c is the first  char‐acter of the value of the IFS variable. 

# If IFS is unset, the parameters are separated by spaces.  If IFS is null, the parameters are joined without intervening separators.

1
2
3
4
5
6
7
oldIFS= "$IFS" 
IFS= " " 
array=($result)
IFS= "$oldIFS" 
for  in  ${array[@]};  do
echo  $i
done

# 使用facter获取一组key-value

# facter的输出有换行符,必须把换行符替换成空格

# 将换行符替换成空格可以使用awk或sed

# awk -v RS="" '{gsub("\n"," ");print}'

# echo -e "2 \n1" | sed ':a;N;$!ba;s/\n/ /g'

1
2
3
result=$(facter |  awk  '/ipaddress/ && !/ipaddress_lo/ {print $1 " " $3}'  awk  - v  RS= ""  '{gsub("\n"," ");print}' )
array=($result)
array_length=${ #array[@]}

# 输出key

1
2
3
for  (( i = 0; i < $array_length; i=i+2 ));  do
     echo  ${array[$i]}
done

# 输出value

1
2
3
for  (( i = 1; i < $array_length; i=i+2 ));  do
     echo  ${array[$i]}
done

# 输出key-value

1
2
3
4
for  (( i = 0; i < $array_length; i=i+2 ));  do
     j=$i+1
     echo  "${array[$i]} - ${array[$j]}"
done

--end--

本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1718284


目录
相关文章
|
4天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
5天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
11 1
|
6天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
25 5
|
8天前
|
Linux Shell 程序员
【Linux】权限(shell运行原理、概念,Linux权限)
【Linux】权限(shell运行原理、概念,Linux权限)
14 2
|
8天前
|
存储 运维 Java
Linux笔记02 —— Shell补充
Linux笔记02 —— Shell补充
31 2
|
8天前
|
安全 Linux Shell
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
19 1
|
8天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
15 3
|
20天前
|
监控 网络协议 数据可视化
Shell脚本查看linux系统性能瓶颈
Shell脚本查看linux系统性能瓶颈
|
28天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
39 1