1.sh script.sh | bash script.sh ##没有执行权限时 2.path/script.sh | ./script.sh ##绝对路径,当前目录下 3.source script.sh | . script.sh ##这种方式会使用source或.号来读如指定shell文件,并会把其余shell中的变量值或函数返回给父shell继续使用
三种方法:shell
方法一:bash
[root@server ~]# a=hello [root@server ~]# echo $a hello
方法二:函数
[root@server ~]# b='hello' [root@server ~]# echo $b hello
方法三:spa
[root@server ~]# c="hello" [root@server ~]# echo $c hello
字符串如何定义?code
[root@server ~]# a=westos-$a [root@server ~]# echo $a westos-hello [root@server ~]# b='westos-$a' [root@server ~]# echo $b westos-$a [root@server ~]# c="westos-$a" [root@server ~]# echo $c westos-westos-hello [root@server ~]# a="westos hello" [root@server ~]# echo $a westos hello
注意:建议没有特别要求时,字符串都加双引号,须要原样输出就加单引号server
$0:获取shell脚本文件名,若是执行时包含路径,则输出脚本路径
$n(>0):获取脚本的第n个参数
$#:获取脚本后参数的总个数
$*:获取全部参数
$@:获取全部参数
$?:获取上一条命令执行状态的返回值,非0为失败
$$:获取当前shell进程号
进程
[root@server mnt]# cat westos.sh #!/bin/bash echo $0 [root@server mnt]# sh westos.sh westos.sh [root@server mnt]# /mnt/westos.sh /mnt/westos.sh
[root@server mnt]# cat westos.sh #!/bin/bash echo $1 $2 [root@server mnt]# sh westos.sh hello westos hello westos [root@server mnt]# sh westos.sh hello redhat hello redhat [root@server mnt]# echo \${1..10} > westos.sh [root@server mnt]# cat westos.sh $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 [root@server mnt]# sh westos.sh {1..10} 1 2 3 4 5 6 7 8 9 10 [root@server mnt]# sh westos.sh {a..z} a b c d e f g h i a0 [root@server mnt]# sh westos.sh {a..z} a b c d e f g h i j
[root@server mnt]# cat westos.sh echo $1 $2 $3 $4 $5 $6 $7 $8 $9 echo $# [root@server mnt]# sh westos.sh {1..100} 1 2 3 4 5 6 7 8 9 100
[root@server mnt]# read str westos hello [root@server mnt]# echo $str westos hello [root@server mnt]# read -p "请输入一个整数:" i 请输入一个整数:10
[root@server mnt]# CMD=`ls -l` [root@server mnt]# echo $CMD total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh [root@server mnt]# CMD=$(ls -l) [root@server mnt]# echo $CMD total 8 -rwxr-xr-x. 1 root root 492 Dec 22 10:25 test.sh -rwxr-xr-x. 1 root root 40 Dec 22 10:40 westos.sh