$0:获取当前执行脚本的文件名,包括路径。
[root@test script]# cat 0.sh
#!/bin/bash
echo $0
[root@test script]# sh 0.sh
0.sh
[root@test script]# cat 0.sh
#!/bin/bash
dirname "$0"
basename "$0"
[root@test script]# sh /byrd/script/0.sh
/byrd/script
0.sh
$n:获取当前执行的shell脚本的第N个参数,n=1..9,当n为0时表示脚本的文件名,如果n大于9,用大括号括起来like${10}.
[root@test script]# cat n.sh
#!/bin/bash
echo $1 $2 ${10}
[root@test script]# sh n.sh a b c d e f g h i j k l m n
a b j
[root@LAMP script]# sh n.sh {a..z}
a b j
[root@test script]# sh n.sh `seq 11`
1 2 10
$*:获取当前shell的所有参数,将所有的命令行参数视为单个字符串。
$@:这个程序的所有参数"$1" "$2" "$3" "...",这是将参数传递给其他程序的最佳方式,因此TA会保留所有内嵌在每个参数里的任何空白。
$#:获取当前shell命令行中参数的总个数。
[root@test script]# cat hashtag.sh
#!/bin/bash
echo "$#"
[root@test script]# sh hashtag.sh
0
[root@test script]# sh hashtag.sh 1 2 3
3
[root@test script]# sh hashtag.sh `seq 300`
300
[root@test script]# cat example.sh
#!/bin/bash
#Example
if [ $# -ne 2 ];then
echo "Error, please enter two parameters."
exit 1
else
echo "You did a good job."
fi
[root@test script]# sh example.sh a
Error, please enter two parameters.
[root@test script]# sh example.sh a b
You did a good job.
[root@test script]# sh example.sh a b c
Error, please enter two parameters.
$_:代表上一个命令的最后一个参数
$$:代表所在命令的PID
[root@LAMP script]# cat dollar.sh
#!/bin/bash
echo "$$" >/tmp/dollar.pid
while true
do
sleep 1
done
[root@LAMP script]# sh dollar.sh
################################################
[root@LAMP ~]# cat /tmp/dollar.pid
1483
[root@LAMP ~]# ps -ef |grep 1483
root 1483 1453 0 14:58 pts/1 00:00:00 sh dollar.sh
root 1532 1483 0 14:58 pts/1 00:00:00 sleep 1
root 1534 1496 0 14:58 pts/0 00:00:00 grep 1483
[root@LAMP ~]# ps -ef |grep dollar
root 1483 1453 0 14:58 pts/1 00:00:00 sh dollar.sh
root 1555 1496 0 14:58 pts/0 00:00:00 grep dollar
$!:代表最后执行的后台命令的PID
$?:代表上一个命令执行是否成功的标志,如果执行成功则$? 为0,否则不为0
[byrd@LAMP script]$ pwd
/byrd/script
[byrd@LAMP script]$ echo $?
0 #运行成功
[byrd@LAMP script]$ ls /root
ls: cannot open directory /root: Permission denied
[byrd@LAMP script]$ echo $?
2 #权限拒绝
[byrd@LAMP script]$ hahaha
-bash: hahaha: command not found
[byrd@LAMP script]$ echo $?
127 #未找到该命令
###########################################
[byrd@LAMP ~]$ cat /byrd/script/question_mark.sh
#!/bin/bash
#Example
ls -al /root >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "User is root"
else
echo "The user is not root"
fi
[root@LAMP script]# sh question_mark.sh
User is root
[root@LAMP script]# su - byrd
[byrd@LAMP ~]$ sh /byrd/script/question_mark.sh
The user is not root
未完成,待整理!
相关推荐
简单的说shell程序就是一个包含若干行shell或者linux命令的文件,象编写高级语言的程序一样,编写一个shell程序需要一个文本编辑器.如Ⅵ等.在文本编辑环境下,依据shell的语法规则,输入一些shell/linux命令行,形成...
在Linux系统中进行日常运维或者是编写脚本时,变量是再熟悉不过的了,但这些变量都有哪些类型,具体的用法又有哪些差异呢?本文整理分享给大家: 一、bash变量类型: 本地变量 局部变量 环境变量 位置变量 特殊变量...
2.Shell编程之变量及LAMP导航菜单编写.mp4 3.Shell编程之IF条件语句各种案例演练.mp4 4.使用if条件语句编写MySQL备份脚本.mp4 5.Shell编程之LAMP一键安装脚本实战.mp4 6.For循环及实战批量远程命令脚本.mp4 7.While...
本文详细论述了,并且通过实例,描述了在shell脚本当中,创建子shell的情况。
在Shell中的特殊变量主要分别两种位置参数变量、状态变量两种。 位置参数变量 Shell中的位置参数变量主要是指0、0、1、$#等,主要用于从命令行、函数或脚本执行等地方传递参数。详细说明如下所示: $0:获取当前...
今天小编就为大家分享一篇linux shell脚本对未定义变量的判断以及if的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
Linux环境变量 ~/.bash_profile && ~/.bashrc 用户登陆Linux操作系统的时候,”/etc/profile”, “~/.bash_profile”等配置文件会被自动执行。 执行过程是这样的: 登陆Linux系统时,首先启动”/etc/profile”,...
shell编程基础-变量详解。 shell编程基础-语句详解。 shell编程基础-函数详解。 shell编程实例分析。 linux应用程序设计基础-GCC编译器。 linux应用程序设计基础-GDB调试器。 linux应用程序设计基础-Makefile。 ...
1、${HOME}:首先, HOME 是个变量,它代表你的 home 目录,变量前必须加上 $ 符号,否则会报错 usr/local/dvsdk为dvsdk的destination folder 由#echo $DVSDK测试环境变量添加成功否 2、想要使得开机时自动加载这个...
shell脚本中28个特殊字符的作用简明总结Shell命令行中特殊字符与其转义详解(去除特殊含义)Shell脚本中的特殊字符(美元符、反斜杠、引号等)作用介绍Shell脚本中的位置变量参数(特殊字符)实例讲解Linux Shell中的...
Linux set命令用于设置shell。 set指令能设置所使用shell的执行方式,可依照不同的需求来做设置。 语法 set 【选项】 【参数】 选项 -a:标示已修改的变量,以供输出至环境变量 -b:使被中止的后台程序立刻回报...
我们大家在平时使用Linux的时候,经常需要配置一些环境变量,这时候一般都是网上随便搜搜就有人介绍经验的。不过问题在于他们的方法各不相同,有人说配置在/etc/profile里,有人说配置在/etc/environment,有人说...
问题描述:在linux shell中如何处理tail -n 10 access.log这样的命令行选项?在bash中,可以用以下三种方式来处理命令行参数,每种方式都有自己的应用场景。1,直接处理,依次对$1,$2,…,$n进行解析,分别手工处理;...
shell编程的详解,包括变量定义,函数调用,条件控制等。
文件为doc版,可自行转成txt,在手机上看挺好的。 本资源来自网络,如有纰漏还请告知,如觉得还不错,请留言告知后来人,谢谢!!!!! 入门学习Linux常用必会60个命令实例详解 Linux必学的60个命令 Linux提供...
Linux启动过程详解 熟悉Linux服务 能够独立安装Linux操作系统 能够熟练使用Linux系统的基本命令 认识Linux系统的常用服务 安装Linux操作系统 Linux基本命令实践 设置Linux环境变量 定制Linux的服务 Shell 编程...
3.2.3 Makefile文件中变量的使用.............................................................................. 51 3.3 二进制代码工具的使用...................................................................
全书共22章,从Linux系统安装开始,讲解了Linux常用命令、Shell编程、C语言基础、指针与函数、C程序网络编程、图形界面程序设计等内容。书中通过大量的实例来讲解知识要点,读者可以通过这些实例和练习对C程序有一个...
5.1.什么是shell变量 58 ※5.2.本地变量 58 ※5.3.环境变量 64 ※5.4.位置变量参数 67 ※七、Linux 进程管理 69 1、程序和进程 69 1.1 进程分类; 69 1.2 进程的属性; 69 1.3 父进程和子进程; 69 2、进程管理...