长春长春网站建设,设计国外网站有哪些,台州网站建设惠店,wordpress怎么做资料库文章目录1.shell语法#xff1a;shell是用C语言编写的程序#xff0c;是用户使用Linux的桥梁#xff0c;硬件内核(os)shell文件系统1.1 变量#xff1a;readonly定义只读变量#xff0c;unset删除变量1.2 函数#xff1a;shell脚本传递的参数中包含空格…
文章目录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、case2.ssh_toolsed -i s/\r//g 文件删除从txt复制来代码的换行符3.build_bmcTo go back to default recipes: devtool reset linux-aspeed , devtool reset u-boot3.1 环境变量用bash调用脚本的时候会创建一个和自己一模一样的shell子进程执行这个外部命令。这个子进程中设置了自己的运行的环境变量此时父进程的环境变量并没有改变。bash test.sh ./test.sh。4.scp_image不用管STRING15.bmc_upgrade断电重启才从主启当前永远mtd46.自动升级bmcAC ON物理上电1.shell语法shell是用C语言编写的程序是用户使用Linux的桥梁硬件内核(os)shell文件系统
1.1 变量readonly定义只读变量unset删除变量 如果字符串中有空格必须需要使用引号strhello 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} inlc1 | lc2 | cmm | fb1 | fb2);;*)usage;;esacdev${1}led_devie${dev%[^a-zA-Z]} # lcif [ $1 ! cmm ];thenindex${dev/*[a-zA-Z]/} # 1fi;;设置别名printf重定向exituntilshiftbasename/dirname 不添加引号转义将不被执行如下转义。 command /dev/null 21不在屏幕上显示输出结果和错误。/dev/null 是一个特殊文件写入到它的内容都会被丢弃从该文件读取内容什么也读不到。
# 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
sum0
until [ $# -eq 0 ]
do
sumexpr $sum $1
shift
done
echo sum is: $sum$./shift1.sh 10 20 15
结果显示45basename /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与letlet命令和双小括号 (( )) 的用法是类似的它们都是用来对整数进行运算不能对小数浮点数或字符串运算。 整型运算如上同如下。 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_toolsed -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 $argv0set 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 cit10.75.159.16expect {*yes/no* {send yes\rexp_continue}*assword:* {if { $passwderror 1 } {puts passwd is errorexit 2}set timeout 1000set passwderror 1send $passwd\rsleep 0.2send sudo su\rsleep 0.2send $passwd\rsleep 0.2send docker exec -it 16d93b0d2026 bash\rsleep 0.2send zsh\rsleep 0.2send ls\rsleep 0.2send cd\rinteract}}
}#11111111111111111111111111111111111111111111111111111111111111111111111111
expect {*yes/no* {send yes\rexp_continue # 该项被匹配后还能继续匹配该expect判断语句内的其他项}*assword:* {if { $passwderror 1 } {puts passwd is errorexit 2}set timeout 1000set passwderror 1send $passwd\rif { $arg_leng 1} {interact # 执行完成后保持交互状态把控制权交给控制台这个时候便可以手动操作。# 如果没有该命令命令完成后即退出。}if { $arg_leng 2} {exp_continue }}*piraspberrypi* {send picocom -b 115200 /dev/ttyUSB$ttyUSBx\rexp_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$ttyUSBxexit 1}*Terminal ready* {interact}
}3.build_bmcTo 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 imageecho $(basename $0) platformecho $(basename $0) platform featureecho $(basename $0) platform cleanecho $(basename $0) platform clean featureechoecho Examples:echo $(basename $0) kestrelecho $(basename $0) kestrel ipmidecho $(basename $0) kestrel cleanecho $(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 inclean)source ./setup $1;echo DL_DIR ? \/home_a/y/usr/local/downloads_ocp\ conf/local.confbitbake 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 developmentchmod 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 grgrep -nr3.1 环境变量用bash调用脚本的时候会创建一个和自己一模一样的shell子进程执行这个外部命令。这个子进程中设置了自己的运行的环境变量此时父进程的环境变量并没有改变。bash test.sh ./test.sh。
用source来执行脚本的时候不会创建子进程而是在父进程中直接执行所以当需要程序修改当前shell本身的环境变量的时候用source命令。source test.sh . test.sh。如下记住ebe从大到小。 上面就是改变linux的env的三种方法每个用户env都不一样env中HOME为用户主目录。 export在命令行单独输入是临时的export…env可查到多出了刚export的但用户登出再登录env查看没有刚export的。但在.bash_profile中都含有export用户一登录就执行.bash_profile所以env中含有这些环境变量这样export可视为永久。 所有可执行程序都要PATH指定如lspwd也是可执行程序不加././是在当前目录执行因为在冒号分隔冒号不是连接的几个目录下找。sqlplus命令行命令在/oracle/bin中。 ls这个可执行程序就是在bin目录下上面PATH已指定。如下若未改为英文显示未找到命令中文。 如下必须在用户主目录/oracle才能vi .bash_profile。 如下对所有用户有效。 /etc/profile 是文件里面有不建议在这文件里修改的英文说明 /etc/profile.d/ 是目录如下新建.sh文件创建全局环境变量。 如下在sss.sh中写入下行。/etc/profile这个文件中有这么一段shellfor i in /etc/profile.d/*.sh会在每次启动时自动加载profile.d目录中每个配置。 不想要什么变量直接删除 /etc/profile.d/ 下对应的shell 脚本即可当用户重新登录shell如下或source /etc/profile 时会触发。
4.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
} 5.bmc_upgrade断电重启才从主启当前永远mtd4
if [ $# -lt 1 ];thenecho Usage: basename $01;2;allecho 1 : flash0echo 2 : flash0roecho all : flash0 flash0roecho ocp : update ocpbmc imageexit 255
fi
cat /proc/mtd |grep -i flash0\
cat /proc/mtd |grep -i flash0roF0_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_stringflashcp ./flash-* /dev/$F0_string
elif [ $1 2 ]; thensource /usr/local/bin/openbmc-utils.shecho Update /dev/$F1_stringflashcp ./flash-* /dev/$F1_string
elif [ $1 all ]; thensource /usr/local/bin/openbmc-utils.shecho Update /dev/$F0_string /dev/$F1_stringecho First : Update /dev/$F0_stringflashcp ./flash-* /dev/$F0_stringecho Second : Update /dev/$F1_stringflashcp ./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 imageexit 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.Activebusctl 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 Overecho reboot now !!!!reboot
elseecho Update fail !!!!
fi6.自动升级bmcAC ON物理上电 #!/bin/sh
#shellcheck disableSC1091
. /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_TIMES8check_file_ok() {if [ ! -f $BMC_WTD2_COUNT ]; then #这行if是假如不存在文件echo 0 $BMC_WTD2_COUNT #创建文件并写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 existexit 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 exceededexit 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 [ Masterx $bmc_bootx ]; 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$((index1))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_versionecho $bmc_wtd2_countecho $wdt2_timeout_countif [ $bmc_wtd2_countx $wdt2_timeout_countx ]; then # NNo WTD没有增加if [ $bmc_base_versionx $current_versionx ]; then # 文件版本和当前版本一致if [ $bmc_base_versionx $other_versionx ]; then # 文件版本和对面版本一致echo all sameclean_bmc_fw_update_countelse # 文件版本和当前版本一致但不和对面版本一致 【只有非当前版本一致】echo only other not sameupdate_bmc_fw_update_countupdate_bmc_fw otherfielse # 文件版本和当前版本不一致if [ $bmc_base_versionx $other_versionx ]; then # 【只有当前BMC版本不一致】echo only current not samechange_bmc_bootelse # 【两块BMC版本都不一致】echo all not sameupdate_bmc_fw_update_countupdate_bmc_fw otherchange_bmc_bootfifielse #Y WTD增加了update_wdt2_timeout_countif [ $bmc_base_versionx $current_versionx ]; then #【当前BMC版本一致】echo current sameupdate_bmc_fw_update_countupdate_bmc_fw otherelse # 【当前BMC版本不一致】echo current not sameupdate_bmc_fw_update_countupdate_bmc_fw otherchange_bmc_bootfifi
}
main