12.Linux shell编程(脚本传参)

简介: (创建于2018/1/31)1.传递参数Press ENTER or type command to continue#!/bin/bash ...

(创建于2018/1/31)

1.传递参数

Press ENTER or type command to continue
#!/bin/bash                                                                              
 
echo $0
echo $1
echo $2

执行命令:
./14.sh hello world bye

输出结果:
./14.sh
hello
world

我们在传递了三个参数,hello world bye,但是脚本中只接到了两个,因为默认第一个参数$0得到
的是当前文件路径,是一个完整路径,如果我们只要得到文件名不要路径怎么做呢

使用basename命令(basename的作用是从文件名中去除目录和后缀,ru执行basename kernel/include/linux/stddef.h得到stddef.h)

  1 #!/bin/bash                                                                              
  2 
  4 filename=$(basename $0) //注意不要有空格
  5 
  6 echo filename
  7 echo $1
  8 echo $2
  
  输出
  
tabstop=8root@iZbp11v3y27wpf6mglp2glZ:/user/renzhenming/shell# ./14.sh hello world bye
filename
hello
world

2.使用$@遍历所有参数

#!/bin/bash                                                                              

echo "the number of params:$#"    //$#获取参数个数

for param in "$@"
do
   echo "param:$param"
done

./14.sh hello=bitch world byebye
输出:

the number of params:3
param:hello=bitch
param:world
param:byebye

getopt命令
。。。

shell中的变量

看下边是一个简单的脚本,定义了三个变量,然后输出,看下下边的结果。我们期望打印的结果是一个数字,两个字符串。但是只是打印了前两个,然后报了一个错误,line 5: zhen: command not found,根据提示可以看到zhen 这个命令没有找到,为什么它把zhen这个字符串当作了命令呢,原因就在于HEHE这个变量后边的值由于没有加双引号,所以只把ren当作了HEHE的值,然后空格后边的zhen被当作了命令对待,牢记一点,shell编程中,空格后边的都会被当作命令对待,慎用空格

  1 #!/bin/bash                                                                        
  2 
  3 NDK=10
  4 JNI="ren zhen ming"
  5 HEHE=ren zhen ming
  6 
  7 echo $NDK
  8 echo $JNI
  9 echo $HEHE

执行结果
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
./ren.sh: line 5: zhen: command not found
10
ren zhen ming

字符串中也可以引用变量值

在下边我把JNI这个变量放在了字符串中进行打印

  1 #!/bin/bash                                                                        
  2 
  3 NDK=10
  4 JNI="ren zhen ming"
  5 HEHE="ren zhen ming"
  6 
  7 echo $NDK
  8 echo $JNI
  9 echo $HEHE
 10 
 11 echo "$JNI is a good man"

tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
10
ren zhen ming
ren zhen ming
ren zhen ming is a good man
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ^C
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# 
将命令执行结果赋值给变量

例如我们想得到当前的时间和当前用户并将它打印出来,这样做怎么样。可以看到命令date和who只是被当作字符串打印了

  1 #!/bin/bash                                                                        
  2 
  3 text=date
  4 
  5 text2=who
  6 
  7 echo $text
  8 echo $text2


tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
date
who
root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# 

那么是不是应该在date命令前加上$,因为我们取变量值就是用的它,试一下,发现什么都没有打印

  1 #!/bin/bash                                                                        
  2 
  3 text=$date
  4 
  5 text2=$who
  6 
  7 echo $text
  8 echo $text2

实际上应该这样做

  1 #!/bin/bash                                                                        
  2 
  3 text=$(date)
  4 
  5 text2=$(who)
  6 
  7 echo $text
  8 echo $text2

  tabstop=8root@iZuf67sz57humoriy3o6oiZ:/usr/ndk/temp# ./ren.sh 
Sat Sep 8 11:10:08 CST 2018
root pts/0 2018-09-08 10:13 (101.88.229.243) root pts/1 2018-09-08 10:34 (101.88.229.243) root pts/2 2018-09-08 11:08 (101.88.229.243)

或者这样做,他们的结果是一样的,注意这个符号不是单引号,而是你键盘右上角esc下边的那个符号

  1 #!/bin/bash                                                                        
  2 
  3 text=`date`
  4 
  5 text2=`who`
  6 
  7 echo $text
  8 echo $text2

相关文章
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
2天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
13 0
|
6天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
28 0
|
7天前
|
监控 Shell 开发工具
Shell编程
Shell编程
|
7天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
8天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
29 1
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
2月前
|
Linux 调度 数据库
Linux下的系统编程——线程同步(十三)
Linux下的系统编程——线程同步(十三)
52 0
Linux下的系统编程——线程同步(十三)
|
6月前
|
存储 Linux 调度
Linux系统编程 多线程基础
Linux系统编程 多线程基础
30 0
|
1月前
|
存储 安全 数据管理
Linux系统编程教程之Linux线程函数的使用:讲解Linux线程函数
Linux系统编程教程之Linux线程函数的使用:讲解Linux线程函数
18 1