您的位置:首页 > 运维架构 > Shell

关于shell变量的第二个shell脚本

2015-08-22 19:55 661 查看
1.文件名为test.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

name=hanyanwei

echo "My name is $name"

echo $UID

echo $PWD

echo "#####################"

echo $0

echo $1

执行

sh test.sh

执行结果:

My name is hanyanwei

0

/root

#####################

test.sh

2.文件名为var.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

name=hanyanwei

echo "My name is $name"

echo $UID

echo $PWD

echo "#####################"

echo $0

echo $1

执行

sh var.sh lkp

执行结果:

My name is hanyanwei

0

/root

#####################

var.sh

lkp

3.文件名为var.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

name=hanyanwei

echo "My name is $name"

echo $UID

echo $PWD

echo "#####################"

echo $0

echo $1 $2

执行

sh var.sh parameter1 parameter2

执行结果:

My name is hanyanwei

0

/root

#####################

var.sh

parameter1 parameter2

4.文件名为var.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

name=hanyanwei

echo "My name is $name"

echo $UID

echo $PWD

echo "#####################"

echo $0

echo $1 $2

执行

sh var.sh 123 abc

执行结果:

My name is hanyanwei

0

/root

#####################

var.sh

123 abc

5.文件名为var.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

echo "#####################"

echo "The \$1 is $1"

echo "The \$2 is $2"

echo "The \$? is $?"

echo "The \$* is $*"

echo "The \$# is $#"

执行

sh var.sh

执行结果:

#####################

The $1 is

The $1 is

The $1 is 0

The $1 is

The $1 is 0

6.文件名为var.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

echo "#####################"

echo "The \$1 is $1"

echo "The \$2 is $2"

echo "The \$? is $?"

echo "The \$* is $*"

echo "The \$# is $#"

执行

sh var.sh abc edf

执行结果:

#####################

The $1 is abc

The $1 is edf

The $1 is 0

The $1 is abc edf
The $1 is 2

$?的讲解



如上图:

我第一次输入执行: kdfjsdklfsd

会提 command not found

然后我再执行echo $?

结果是127

注解:127是错误码

127是命令没找到,command not found

然后我在执行: df -f

然后我再执行:echo $?

结果是0

注解:0表示程序执行成功了

只要返回值是 0,就代表程序执行成功了~

7.文件名为var1.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

echo -e "\033[32m---------------------------\033[0m"

echo "This is $0 param !"

echo "This \$1 is $1 param !"

echo "This \$2 is $2 param !"

echo -e "\033[32m---------------------------\033[0m"

echo "This \$* is $* param !"

echo "This \$# is $# param !"

echo "This \$? is $? param !"

执行

sh var1.sh abc edf

执行结果:



8.文件名为var2.sh

#!/bin/bash

#define path variables

#by authors lkp 2015-08-22

echo -e "\033[32mPlease select Menu follow:\033[1m"

echo "1)安装apache服务器."

echo "2)安装Mysql服务器."

echo "1)安装php服务器."

echo "1)安装LAMP WEB架构."

echo "-----------------------------------------"

执行

sh var2.sh 

执行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 变量