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

linux shell 脚本命令学习笔记

2017-12-17 17:29 741 查看

1. set 的使用: debug调试的时候用的

linux shell 脚本编写好要经过漫长的调试阶段,可以使用sh -x 执行。但是这种情况在远程调用脚本的时候,就有诸多不便。又想知道脚本内部执行的变量的值或执行结果,这个时候可以使用在脚本内部用 set -x 。

set去追踪一段代码的显示情况,执行后在整个脚本有效

set -x 开启 

set +x关闭

set -o 查看

例子:

sxj@sxj-PC MINGW64 /d/mSysGit_workspace/linux shell script/3760_01_code

$ cat de.sh

#!/bin/bash

#Filename: debug.sh

for i in {1..6};

do

set -x

        echo $i;

        echo "#$i test done!";

set +x

done

echo "Script executed"

sxj@sxj-PC MINGW64 /d/mSysGit_workspace/linux shell script/3760_01_code

$ sh de.sh

+ echo 1

1

+ echo '#1 test done!'

#1 test done!

+ set +x

+ echo 2

2

+ echo '#2 test done!'

#2 test done!

+ set +x

+ echo 3

3

+ echo '#3 test done!'

#3 test done!

+ set +x

+ echo 4

4

+ echo '#4 test done!'

#4 test done!

+ set +x

+ echo 5

5

+ echo '#5 test done!'

#5 test done!

+ set +x

+ echo 6

6

+ echo '#6 test done!'

#6 test done!

+ set +x

Script executed

可以看到,针对每一条每一条命令,都会先跟踪一遍,就是显示一遍,然后执行一遍;

2. stty命令

stty [ -a ]
-g ] [ Options ]
  stty(set tty)命令用于显示和修改当前注册的终端的属性。

UNIX系统为键盘的输入和终端的输出提供了重要的控制手段,可以通过stty命令对特定终端或通信线路设置选项。 在stty中相应选项前冠以负号(-),该选项被清除;如果无负号,该选项被设置。

stty -a #将所有选项设置的当前状态写到标准输出中
old_stty_settings=`stty -g` #保存当前设置
stty "$old_stty_settings" #恢复当前设置
stty -echo #禁止回显,当您在键盘上输入时,并不出现在屏幕上

stty echo #打开回显
stty raw #设置原始输入
stty -raw #关闭原始输入
stty igncr #开启忽略回车符
stty -igncr#关闭忽略回车符
一般,在让用户输入密码的时候肯定是要关闭回显的,如果不关闭就是明文输入了。所以stty -echo 功能的主要用途就是密码输入时的交互场景。输完密码之后,再使用stty echo打开回显。

例子:

sxj@sxj-PC MINGW64 /d/mSysGit_workspace/linux shell script/3760_01_code

$ cat password.sh

#!/bin/sh

#Filename: password.sh

echo -n "Enter password:"

stty -echo

read password

stty echo

echo

echo Password read.

echo Password is : $password

sxj@sxj-PC MINGW64 /d/mSysGit_workspace/linux shell script/3760_01_code

$ sh password.sh

Enter password:

Password read.

Password is : 1234R

sxj@sxj-PC MINGW64 /d/mSysGit_workspace/linux shell script/3760_01_code

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