shell script的基本使用.

一:认识 shell script 的作用:

(1):运行方式:

   1:直接命令下达:shell.sh文件必须要具备可读与可运行(rx)的权限。

      •绝对路径:使用/home/dmtsai/first.sh来下达命令。

      •相对路径:假设工作目录在/home/dmtsai/,则使用./first.sh来运行。

      •变量“PATH”功能:将first.sh放在PATH指定的目录内,如~/bin/

   2:以bash程序来运行:通过“bash first.sh”或“sh first.sh”来运行。

   3:使用-n及-x来检查与追踪shell文件的语法是否正确。

   4:编写第一个shell script:

    

 运行:

[root@server1 dir]# sh first.sh
Hi Mr dengan
[root@server1 dir]# ./first.sh
Hi Mr dengan
[root@server1 dir]# bash first.sh
Hi Mr dengan
[root@server1 dir]# echo $? //通过exit 1:来表达当程序正确执行完后给系统传回一个1,保存在$?中.
1

export PATH :是将其设为全局变量,
#!/bin/bash //在宣告这个script使用的shell名称。
PATH:可让这个程序在运行时直接执行一些外部命令,而不必写绝对路径。

(2):输入:

session.sh

#!/bin/bash #Introduce: #User inputs his name and age.program show it in screen. #History: #2020/3/15 PATH=/bin:/sbin/:/usr/bin/:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH read -p "please input your name:" name #提示使用者输入 read -p "Input your age:" age echo -e "\a\n hello $name" echo -e "Your age is $age" exit 1 ~
操作:
[root@server1 dir]# sh session.sh
please input your name:deng
Input your age:12

 hello deng
Your age is 12

猜你喜欢

转载自www.cnblogs.com/1314bjwg/p/12463968.html