在shell脚本中利用expect实现自动应答
上一篇 / 下一篇 2011-06-12 23:14:44 / 个人分类:Shell学习
TAG:
- 引用 删除 abinNO1 / 2013-04-18 20:25:21
-
获取登陆密码:
#!/bin/bash
clear
sed -e "s/Kernel.*$/Kernel $(uname -r)/" /etc/issue
echo -n "login:"
read login
echo -n "assword:"
stty -echo
read passwd
stty sane
mail $USER <<- fin
loginlogin
passwdpasswd
fin
echo -n -e "\nLast login: $(date) from $(who |awk '{print $6}'|head -n1|awk -F "[()]" '{print $2}') \n"
sleep 1
exit 0
- 引用 删除 abinNO1 / 2013-01-07 19:26:02
-
function getchar {
stty cbreak -echo
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -cbreak echo
}
echo -ne '\e[6n';read -sdR pos
pos=${pos#*[}
row=$(echo $pos|awk -F ';' '{print $1-1}')
col=$(echo $pos|awk -F ';' '{print $2-1}')
tput cup $row $col
echo -n -e "please input password:"
CURPOS=21
while true
do
CURPOS=`expr ${CURPOS} + 1`
char=$(getchar)
tput cup $row ${CURPOS}
if [ -z "$char" ]
then
break
else
PWDSTR=${PWDSTR}${char}
fi
echo -n "*"
done
echo -e "\nyour password: ${PWDSTR}"
- 引用 删除 abinNO1 / 2011-09-08 21:19:39
-
http://blog.csdn.net/xuhongning/article/details/6191515
shell中的变量替换
- 引用 删除 abinNO1 / 2011-07-27 21:23:20
-
${file#*/}:拿掉第一條 / 及其左邊的字串:dir1/dir2/dir3/my.file.txt
${file##*/}:拿掉最後一條 / 及其左邊的字串:my.file.txt
${file#*.}:拿掉第一個 . 及其左邊的字串:file.txt
${file##*.}:拿掉最後一個 . 及其左邊的字串:txt
${file%/*}:拿掉最後條 / 及其右邊的字串:/dir1/dir2/dir3
${file%%/*}:拿掉第一條 / 及其右邊的字串:(空值)
${file%.*}:拿掉最後一個 . 及其右邊的字串:/dir1/dir2/dir3/my.file
${file%%.*}:拿掉第一個 . 及其右邊的字串:/dir1/dir2/dir3/my
記憶的方法為:
# 是去掉左邊(在鑑盤上 # 在 $ 之左邊)
% 是去掉右邊(在鑑盤上 % 在 $ 之右邊)
單一符號是最小匹配﹔兩個符號是最大匹配。
${file:0:5}:提取最左邊的 5 個字?:/dir1
${file:5:5}:提取第 5 個字?右邊的連續 5 個字?:/dir2
我們也可以對變量值裡的字串作替換:
${file/dir/path}:將第一個 dir 提換為 path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:將全部 dir 提換為 path:/path1/path2/path3/my.file.txt
利用 ${ } 還可針對不同的變數狀態賦值(沒設定、空值、非空值):
${file-my.file.txt} :假如 $file 為空值,則使用 my.file.txt 作默認值。(保留沒設定及非空值)
${file:-my.file.txt} :假如 $file 沒有設定或為空值,則使用 my.file.txt 作默認值。 (保留非空值)
${file+my.file.txt} :不管 $file 為何值,均使用 my.file.txt 作默認值。 (不保留任何值)
${file:+my.file.txt} :除非 $file 為空值,否則使用 my.file.txt 作默認值。 (保留空值)
${file=my.file.txt} :若 $file 沒設定,則使用 my.file.txt 作默認值,同時將 $file 定義為非空值。 (保留空值及非空值)
${file:=my.file.txt} :若 $file 沒設定或為空值,則使用 my.file.txt 作默認值,同時將 $file 定義為非空值。 (保留非空值)
${file?my.file.txt} :若 $file 沒設定,則將 my.file.txt 輸出至 STDERR。 (保留空值及非空值))
${file:?my.file.txt} :若 $file 沒設定或為空值,則將 my.file.txt 輸出至 STDERR。 (保留非空值)
還有哦,${#var} 可計算出變量值的長度:
${#file} 可得到 27 ,因為 /dir1/dir2/dir3/my.file.txt 剛好是 27 個字?...
接下?恚贋榇蠹医樯砸幌 bash 的組數(array)處理方法。
一般而言,A="a b c def" 這樣的變量只是將 $A 替換為一個單一的字串,
但是改為 A=(a b c def) ,則是將 $A 定義為組數...
bash 的組數替換方法可參考如下方法:
${A[@]} 或 ${A[*]} 可得到 a b c def (全部組數)
${A[0]} 可得到 a (第一個組數),${A[1]} 則為第二個組數...
${#A[@]} 或 ${#A[*]} 可得到 4 (全部組數數量)
${#A[0]} 可得到 1 (即第一個組數(a)的長度),${A[3]} 可得到 3 (第一個組數(def)的長度)
A[3]=xyz 則是將第 4 個組數重新定義為 xyz ...
以用 ${ } 分別替換獲得不同的值:
我的栏目
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | ||||||
5 | 6 | 7 | 8 | 9 | 10 | 11 | |||
12 | 13 | 14 | 15 | 16 | 17 | 18 | |||
19 | 20 | 21 | 22 | 23 | 24 | 25 | |||
26 | 27 | 28 | 29 | 30 | 31 |
我的存档
数据统计
- 访问量: 124800
- 日志数: 67
- 建立时间: 2010-12-07
- 更新时间: 2019-08-28