網站建設 方案書網絡平臺怎么推廣
文章目錄
- 1.shell語法:Shell是用C語言編寫的程序,它是用戶使用Linux的橋梁,硬件>內核(os)>shell>文件系統
- 1.1 變量:readonly定義只讀變量,unset刪除變量
- 1.2 函數:shell腳本傳遞的參數中包含空格,應使用單引號或雙引號將該參數括起來,以便于腳本將這個參數作為整體來接收。if [ -n str1 ] 當字符串的長度大于0時為真(字符串非空)
- 1.3 運算:算術、關系、布爾、邏輯、字符串、文件運算
- 1.4 流程控制:if、for、while、case
- 2.ssh_tool:sed -i 's/\r//g' 文件(刪除從txt復制來代碼的換行符)
- 3.build_bmc:To go back to default recipes: devtool reset linux-aspeed , devtool reset u-boot
- 4.環(huán)境變量:用bash調用腳本的時候會創(chuàng)建一個和自己一模一樣的shell子進程執(zhí)行這個外部命令。這個子進程中設置了自己的運行的環(huán)境變量,此時父進程的環(huán)境變量并沒有改變。bash test.sh == ./test.sh。
- 5.scp_image:不用管STRING1
- 6.bmc_upgrade:斷電重啟才從主啟,當前永遠mtd4
- 7.自動升級bmc:AC ON物理上電
1.shell語法:Shell是用C語言編寫的程序,它是用戶使用Linux的橋梁,硬件>內核(os)>shell>文件系統
1.1 變量:readonly定義只讀變量,unset刪除變量
如果字符串中有空格,必須需要使用引號(str=hello world會報錯)。
獲取字符串長度:echo ${#str}
,expr length “${str}”
。
截取字符串:echo ${str:1:4}
:顯示字符串第1到第4個字符。
echo ${str:4}
:從左邊第4個字符開始,一直到結束。
echo ${str:0-6:3}
:從倒數第6個字符開始的3個字符。
echo ${str:0-6}
:從倒數第6個字符開始,一直到結束。
file=/dir1/dir2/dir3/my.file.txt
${file#*/}:刪掉第一個 / 及其左邊的字符串:dir1/dir2/dir3/my.file.txt / 可換成 . 即#*.
${file##*/}:刪掉最后一個 / 及其左邊的字符串:my.file.txt${file%/*}:刪掉最后一個 / 及其右邊的字符串:/dir1/dir2/dir3
${file%%/*}:刪掉第一個 / 及其右邊的字符串:(空值)
#!/bin/bash
for i in `ifconfig | grep -o ^[a-z0-9.]*`
doname=$i echo $nameipaddr=$(ifconfig $i|sed -n 2p|awk '{ print $2 }'|tr -d 'addr:') # -d刪除echo http://$1:8080/api/slave -d '{"slave":"'$2${name}'","ip":"'${ipaddr}'"}'
done
如下截取 = 號左邊即第一個。
"-d")shiftcase ${1} in"lc1" | "lc2" | "cmm" | "fb1" | "fb2");;*)usage;;esacdev=${1}led_devie=${dev%[^a-zA-Z]} # lcif [ "$1" != "cmm" ];thenindex=${dev/*[a-zA-Z]/} # 1fi;;
設置別名,printf,重定向,exit,until,shift,basename/dirname
不添加引號,轉義將不被執(zhí)行,如下轉義。
command > /dev/null 2>&1
不在屏幕上顯示輸出結果
和錯誤
。/dev/null 是一個特殊文件,寫入到它的內容都會被丟棄,從該文件讀取內容,什么也讀不到。
ps -ef | grep memtester | grep -v grep | cut -c 9-16 | xargs kill -9
# a.sh
a()
{return 1
}
if ! a ; thenecho "111"
fi$ ./a.sh
111
# shift.sh
until [ $# -eq 0 ]
do
echo "第一個參數為: $1 參數個數為: $#"
shift
done$./shift.sh 1 2 3 4
第一個參數為: 1 參數個數為: 4
第一個參數為: 2 參數個數為: 3
第一個參數為: 3 參數個數為: 2
第一個參數為: 4 參數個數為: 1
# shift1.sh
sum=0
until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo "sum is: $sum"$./shift1.sh 10 20 15
結果顯示:45
basename /usr/local/nginx/conf/nginx.conf
nginx.conf
basename -s .conf /usr/local/nginx/conf/nginx.conf
nginxdirname //
/
dirname /a/b/
/a
dirname a
.
dirname a/b
arealpath $path : 返回$path的絕對路徑,路徑不存在會報錯,文件不存在不會報錯
declare與let:let命令和雙小括號 (( )) 的用法是類似的,它們都是用來對整數進行運算,不能對小數(浮點數)或字符串運算。
整型運算如上同如下。
test:用于檢查某個條件是否成立,它可以進行數值、字符、文件三個方面的測試
1.2 函數:shell腳本傳遞的參數中包含空格,應使用單引號或雙引號將該參數括起來,以便于腳本將這個參數作為整體來接收。if [ -n str1 ] 當字符串的長度大于0時為真(字符串非空)
1.3 運算:算術、關系、布爾、邏輯、字符串、文件運算
算術:
關系:
布爾:
邏輯:
文件測試運算:
1.4 流程控制:if、for、while、case
if:
for:
while:
until:
case:
break與continue:
2.ssh_tool:sed -i ‘s/\r//g’ 文件(刪除從txt復制來代碼的換行符)
#!/usr/bin/expect
# b文件,llength等是expect中專屬的,和ftp那些命令一樣
# obmc-server:~$ whereis expect
# expect: /usr/bin/expect /usr/share/man/man1/expect.1.gz
puts "$argv0"set arg_leng [llength $argv]
puts "$arg_leng" set argv_0 [lindex $argv 0]
puts "$argv_0"# @obmc-server:~/test$ ./b w
# ./b
# 1
# w
#!/usr/bin/expect
# 如上一行必須加且必須在第一行
if { [llength $argv] < 1} {puts "Usage:"puts "$argv0 raspberr ttyUSBx <pi2 0>" # ssh-tool.sh pi2 0puts "$argv0 raspberr <pi1> <pi2> <pi3>" # ssh-tool.sh pi1puts "$argv0 project_name <hollywood> <s3ip-bmc> <s3ip-bsp>" # ssh-tool.sh hollywood# puts "$argv0 raspberrNUM ttyUSBx [force]" # ssh-tool.sh pi1 0 force# puts "force if other used tty will kill process,殺串口進程"exit 1
}set arg_leng [llength $argv ]
set argv_0 [lindex $argv 0]
set ttyUSBx [lindex $argv 1]
# set force [lindex $argv 2]
set timeout 20
# set pi3_ip [exec sh -c {curl -s http://10.75.92.228:8080/api/help | python -m json.tool |grep -i "pi3" |cut -d '"' -f 4}]
set pi3_ip 10.75.159.104
set local_file ./tmp/deploy/images/obmc-cl/flash-obmc-cl
set passwderror 0#11111111111111111111111111111111111111111111111111111111111111111111111111
if { $argv_0 == "pi1" } {set passwd 123456spawn ssh pi@$pi1_ip
}
if { $argv_0 == "pi2" } {set passwd 123456spawn ssh pi@$pi2_ip
}
if { $argv_0 == "pi3" } {set passwd 123456spawn ssh pi@$pi3_ip
}
if { $argv_0 == "hollywood" } {set passwd 0penBmcspawn ssh-keygen -f "/home_a/yu/.ssh/known_hosts" -R $hollywood_ipspawn ssh root@$hollywood_ip
}
if { $argv_0 == "docker" } {set passwd 1spawn ssh cit@10.75.159.16expect {"*yes/no*" {send "yes\r"exp_continue}"*assword:*" {if { $passwderror == 1 } {puts "passwd is error"exit 2}set timeout 1000set passwderror 1send "$passwd\r"sleep 0.2send "sudo su\r"sleep 0.2send "$passwd\r"sleep 0.2send "docker exec -it 16d93b0d2026 bash\r"sleep 0.2send "zsh\r"sleep 0.2send "ls\r"sleep 0.2send "cd\r"interact}}
}#11111111111111111111111111111111111111111111111111111111111111111111111111
expect {"*yes/no*" {send "yes\r"exp_continue # 該項被匹配后,還能繼續(xù)匹配該expect判斷語句內的其他項}"*assword:*" {if { $passwderror == 1 } {puts "passwd is error"exit 2}set timeout 1000set passwderror 1send "$passwd\r"if { $arg_leng == 1} {interact # 執(zhí)行完成后保持交互狀態(tài),把控制權交給控制臺,這個時候便可以手動操作。# 如果沒有該命令,命令完成后即退出。}if { $arg_leng == 2} {exp_continue }}"*pi@raspberrypi*" {send " picocom -b 115200 /dev/ttyUSB$ttyUSBx\r"exp_continue}"*FATAL: cannot lock*" {# if { $force == "force" } {# puts "you can kill process and try again"# exit 1# }send " ps -ef |grep -i ttyUSB$ttyUSBx\r "interact}"*FATAL: cannot open*" {puts "!!! ######################################## !!!"puts "!!!no ttyUSB$ttyUSBx"exit 1}"*Terminal ready*" {interact}
}
3.build_bmc:To go back to default recipes: devtool reset linux-aspeed , devtool reset u-boot
#!/bin/bash# downloads() {
# echo "buildpath: [$buildpath]"
# build_exist=$(grep "DL_DIR" $buildpath/conf/local.conf)
# echo "buildexist: [$build_exist]"
# if [ ! -n "$build_exist" ]; then
# echo "DL_DIR ?= \"/home_a/y/usr/local/downloads\"" >> "conf/local.conf"
# fi
# }# buildplatform() {
# source openbmc-init-build-env meta-huaqin/meta-$1 build-$1
# buildpath=$(pwd)
# downloads
# bitbake $1-image
# rm -rf conf/local.conf
# }usage() {echo "Usage: build an openbmc image"echo " $(basename $0) <platform>"echo " $(basename $0) <platform> <feature>"echo " $(basename $0) <platform> <clean>"echo " $(basename $0) <platform> <clean> <feature>"echoecho "Examples:"echo " $(basename $0) kestrel"echo " $(basename $0) kestrel ipmid"echo " $(basename $0) kestrel clean"echo " $(basename $0) kestrel clean ipmid"
}
# if [ $# -eq 1 ]; then# buildplatform $1
if [ $# -eq 1 ]; thensource ./setup $1;echo "DL_DIR ?= \"/home_a/y/usr/local/downloads_ocp\"" >> "conf/local.conf" # openbmc-hollywood-master/build/conf/local.conf 或 meta-huaqin/meta-xs9880-8c/conf/local.conf.samplebitbake obmc-phosphor-image
elif [ $# -ge 2 ]; thencase $2 in"clean")source ./setup $1;echo "DL_DIR ?= \"/home_a/y/usr/local/downloads_ocp\"" >> "conf/local.conf"bitbake obmc-phosphor-image -c cleanall obmc-phosphor-image;;"u-boot")# source openbmc-init-build-env meta-huaqin/meta-$1 build-$1 && buildpath=$(pwd); downloads; bitbake u-boot;;"linux-aspeed")# source openbmc-init-build-env meta-huaqin/meta-$1 build-$1 && buildpath=$(pwd); downloads; bitbake linux-aspeed;;*)source openbmc-init-build-env meta-huaqin/meta-$1 build-$1 && buildpath=$(pwd); downloads; bitbake $2;;esac
elseusage
fi
exit 0
# build目錄下獲取源碼:devtool modify linux-aspeed ,devtool modify u-boot
# This will create local Linux package under /workspace/sources/linux-aspeed for development
chmod 777 /home_a/y/app/bin/(usr/local/bin/)build_bmc~$ vi .bash_profile
export PATH=/home_a/y/app/bin:$PATH~$ vi .bashrc
alias gr='grep -nr'
4.環(huán)境變量:用bash調用腳本的時候會創(chuàng)建一個和自己一模一樣的shell子進程執(zhí)行這個外部命令。這個子進程中設置了自己的運行的環(huán)境變量,此時父進程的環(huán)境變量并沒有改變。bash test.sh == ./test.sh。
用source來執(zhí)行腳本的時候,不會創(chuàng)建子進程,而是在父進程
中直接執(zhí)行,所以當需要程序修改當前shell本身的環(huán)境變量的時候,用source命令。source test.sh == . test.sh
。如下記住ebe從大到小。
上面就是改變linux的env的三種方法,每個用戶env都不一樣
,env中HOME為用戶主目錄。
export在命令行單獨輸入是臨時的(export…,env可查到多出了剛export的,但用戶登出再登錄,env查看沒有剛export的)。但在.bash_profile中都含有export,用戶一登錄就執(zhí)行.bash_profile
,所以env中含有這些環(huán)境變量,這樣export可視為永久。
所有可執(zhí)行程序
都要PATH指定,如ls,pwd(也是可執(zhí)行程序)不加./(./是在當前目錄執(zhí)行),因為在冒號分隔(冒號不是連接)的幾個目錄下找。sqlplus命令行命令在/oracle/bin中。
ls這個可執(zhí)行程序就是在bin目錄下,上面PATH已指定。如下若未改為英文顯示:未找到命令(中文)。
如下必須在用戶主目錄/oracle,才能vi .bash_profile。
如下對所有用戶有效。
/etc/profile 是文件(里面有不建議在這文件里修改的英文說明), /etc/profile.d/ 是目錄(如下新建.sh文件創(chuàng)建全局環(huán)境變量)。
如下在sss.sh中寫入下行。/etc/profile這個文件中有這么一段shell:for i in /etc/profile.d/*.sh;,會在每次啟動時自動加載profile.d目錄中每個配置。
不想要什么變量直接刪除 /etc/profile.d/ 下對應的shell 腳本
即可,當用戶重新登錄shell如下或source /etc/profile 時會觸發(fā)。
5.scp_image:不用管STRING1
#!/usr/bin/expect if { [llength $argv] < 2} { puts "Usage:" puts "$argv0 <Image path> IP" puts "$argv0 <Image path> IP <path>" exit 1
}set timeout 20
set Local_File [lindex $argv 0]
set IP [lindex $argv 1]
set STRING 0
spawn ssh-keygen -f "/home_a/y/.ssh/known_hosts" -R $IPproc myscpfunc { STRING1 } {set passwd 0penBmcset passwderror 0 expect { "*assword:*" { if { $passwderror == 1 } { puts "passwd is error" exit 2 } set timeout 1000 set passwderror 1 send "$passwd\r" exp_continue } "yes/no" { send "yes\r" exp_continue } timeout { puts "connect is timeout" exit 3 }}
}if { [llength $argv] == 2} { if {[regexp -nocase "obmc-phosphor-image" $Local_File]} {spawn scp $Local_File root@$IP:/tmp/imagesmyscpfunc $STRINGspawn scp /home_a/y/bak/bmc_upgrade root@$IP:~/myscpfunc $STRING} else {spawn scp $Local_File /home_a/y/bak/bmc_upgrade root@$IP:~/myscpfunc $STRING}
}if { [llength $argv] == 3} { set path [lindex $argv 2]spawn scp $Local_File root@$IP:$pathmyscpfunc $STRING
}
6.bmc_upgrade:斷電重啟才從主啟,當前永遠mtd4
if [ $# -lt 1 ];thenecho "Usage: `basename $0`+<1;2;all>"echo "1 : flash0"echo "2 : flash0ro"echo "all : flash0 & flash0ro"echo "ocp : update ocpbmc image"exit 255
fi
cat /proc/mtd |grep -i "flash0\""
cat /proc/mtd |grep -i "flash0ro"F0_string=$(cat /proc/mtd |grep -i flash0\" |cut -b 1-4)
F1_string=$(cat /proc/mtd |grep -i flash0ro |cut -b 1-4)
if [ "$F1_string" = "" ]; thenF1_string=$(cat /proc/mtd |grep -i flash1 |cut -b 1-4)
fiif [ $1 == "1" ]; thensource /usr/local/bin/openbmc-utils.shecho "Update /dev/$F0_string"flashcp ./flash-* /dev/$F0_string
elif [ $1 == "2" ]; thensource /usr/local/bin/openbmc-utils.shecho "Update /dev/$F1_string"flashcp ./flash-* /dev/$F1_string
elif [ $1 == "all" ]; thensource /usr/local/bin/openbmc-utils.shecho "Update /dev/$F0_string & /dev/$F1_string"echo "First : Update /dev/$F0_string"flashcp ./flash-* /dev/$F0_stringecho "Second : Update /dev/$F1_string"flashcp ./flash-* /dev/$F1_string
elif [ $1 == "ocp" ]; thenImage_ID=$(ls /tmp/images/)if [ ${#Image_ID} -eq 8 ];thenecho "IMAGE ID = ${Image_ID}"elseecho "IMAGE ID = ${Image_ID}"echo "Error-----multiple image"exit 255fiecho "busctl set-property xyz.openbmc_project.Software.BMC.Updater /xyz/openbmc_project/software/$Image_ID xyz.openbmc_project.Software.Activation RequestedActivation s xyz.openbmc_project.Software.Activation.RequestedActivations.Active"busctl set-property xyz.openbmc_project.Software.BMC.Updater /xyz/openbmc_project/software/$Image_ID xyz.openbmc_project.Software.Activation RequestedActivation s xyz.openbmc_project.Software.Activation.RequestedActivations.Activesleep 3echo "busctl get-property xyz.openbmc_project.Software.BMC.Updater /xyz/openbmc_project/software/$Image_ID xyz.openbmc_project.Software.Activation Activation" busctl get-property xyz.openbmc_project.Software.BMC.Updater /xyz/openbmc_project/software/$Image_ID xyz.openbmc_project.Software.Activation Activationsleep 1
fiif [ $? == "0" ];thenecho "Update Over"echo "reboot now !!!!"reboot
elseecho "Update fail !!!!"
fi
7.自動升級bmc:AC ON物理上電
#!/bin/sh
#shellcheck disable=SC1091
. /usr/local/bin/openbmc-utils.sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/binBMC_VERSION=/var/log/automatic_upgrade/version/bmc_version #主備版本都和這個比較,而不是主備互相比較
BMC_FW=/var/log/automatic_upgrade/fw/flash-xs9880-8c
BMC_WTD2_COUNT=/var/log/wtd_count
BMC_FW_UPDATE_COUNT=/var/log/bmc_update_count
FW_UPDATE_TIMES=8check_file_ok() {if [ ! -f "$BMC_WTD2_COUNT" ]; then #這行if是:假如不存在文件echo 0 > $BMC_WTD2_COUNT #創(chuàng)建文件并寫0fiif [ ! -f "$BMC_FW_UPDATE_COUNT" ]; thenecho 0 > $BMC_FW_UPDATE_COUNTfiif [ ! -f "$BMC_VERSION" ] || [ ! -f "$BMC_FW" ]; thenecho "$BMC_VERSION or $BMC_FW file does not exist"exit 255fi
}check_fw_update_count() {bmc_fw_update_cont=$(cat "$BMC_FW_UPDATE_COUNT")if [ "$FW_UPDATE_TIMES" -lt "$bmc_fw_update_cont" ]; thenlogger -p user.err "$0 update bmc times exceeded"exit 255fi
}check_wtd2_count() {if [ "$wdt2_timeout_count" -eq 0 ]; thenecho 0 > $BMC_WTD2_COUNTfi
}#11111111111111111111111111111111111111111111111111111111111111111111111111111
update_bmc_fw() {if [ "$1" = "current" ]; thenflashcp $BMC_FW /dev/mtd4elif [ "$1" = "other" ]; thenflashcp $BMC_FW /dev/mtd9fi
}change_bmc_boot() {if [ "Master"x = "$bmc_boot"x ]; then bmc_boot_info.sh reset slaveelsebmc_boot_info.sh reset masterfi
}get_bmc_version() {if [ "$1" = "other" ]; thenbmc_version=$(version_dump --pingpong | awk -F ": " '{print $2}')elif [ "$1" = "current" ]; thenbmc_version=$(version_dump bmc | awk -F ": " '{print $2}')fiecho "$bmc_version"
}#1111111111111111111111111111111111111111111111111111111111111111111111111111111
update_bmc_fw_update_count() {index=$(cat "$BMC_FW_UPDATE_COUNT")index=$((index+1))echo $index > $BMC_FW_UPDATE_COUNT
}update_wdt2_timeout_count() {echo "$wdt2_timeout_count" > $BMC_WTD2_COUNT
}clean_bmc_fw_update_count() {echo 0 > $BMC_FW_UPDATE_COUNT
}#111111111111111111111111111111111111111111111111111111111111111111111111111111
main() {check_file_okbmc_boot_info=$(bmc_boot_info.sh)bmc_boot=$(echo $bmc_boot_info |awk -F " " '{print $10}')wdt2_timeout_count=$(echo $bmc_boot_info |awk -F " " '{print $4}')bmc_base_version=$(cat "$BMC_VERSION")bmc_wtd2_count=$(cat "$BMC_WTD2_COUNT")current_version=$(get_bmc_version current)other_version=$(get_bmc_version other)check_wtd2_countcheck_fw_update_countecho "$current_version" echo "$other_version"echo "$bmc_wtd2_count"echo "$wdt2_timeout_count"if [ "$bmc_wtd2_count"x = "$wdt2_timeout_count"x ]; then # N(No) WTD沒有增加if [ "$bmc_base_version"x = "$current_version"x ]; then # 文件版本和當前版本一致if [ "$bmc_base_version"x = "$other_version"x ]; then # 文件版本和對面版本一致echo "all same"clean_bmc_fw_update_countelse # 文件版本和當前版本一致,但不和對面版本一致 【只有非當前版本一致】echo "only other not same"update_bmc_fw_update_countupdate_bmc_fw otherfielse # 文件版本和當前版本不一致if [ "$bmc_base_version"x = "$other_version"x ]; then # 【只有當前BMC版本不一致】echo "only current not same"change_bmc_bootelse # 【兩塊BMC版本都不一致】echo "all not same"update_bmc_fw_update_countupdate_bmc_fw otherchange_bmc_bootfifielse #Y WTD增加了update_wdt2_timeout_countif [ "$bmc_base_version"x = "$current_version"x ]; then #【當前BMC版本一致】echo "current same"update_bmc_fw_update_countupdate_bmc_fw otherelse # 【當前BMC版本不一致】echo "current not same"update_bmc_fw_update_countupdate_bmc_fw otherchange_bmc_bootfifi
}
main