须知:
1、本次 Shell 主要是针对于 OpneStack 环境下的编程练习
2、欢迎大家留言讨论
3、持续更新
练习1:将文件内容打印输出到屏幕上,并在每一行前面加上字符串“nova delete ”,示例如下:
文件内容为:
aaaa
bbbb
cccc
屏幕上输出为:
nova delete aaaa
nova delete bbbb
nova delete cccc
#!/bin/bash #文件功能:文件内容打印输出到屏幕上,并在每一行前面加上字符串“nova delete ” WORK_PATH="/root/gsd/shell" function method1() { for i in `cat $WORK_PATH/file1` do echo "nova delete "$i done } function method2() { sed ‘s/^/nova delete /g‘ $WORK_PATH/file1 } method2
练习2:创建一台虚拟机,并将此虚拟机的id、所在物理机、创建时间输出到文件里
#!/bin/bash #文件功能:创建一台虚拟机,并将此虚拟机的id、所在物理机、创建时间输出到文件result里 WORK_PATH="/root/gsd/shell" #声明环境变量 source /root/.admin-openrc.sh #判断日志文件是否存在 LOG_FILE=$WORK_PATH/instance.log if [ ! -f $LOG_FILE ];then touch $LOG_FILE printf "%-40s%-15s%-25s%-10s%s\n" "UUID" "NAME" "CREATE_TIME" "NODE" "VNC" > $LOG_FILE fi #虚拟机类型 FLAVOR=2C4G #子网ID NET_ID=141247b7-30cc-49ca-a485-578ce3d4ece2 #镜像ID IMG_ID=e7348615-8de4-49e6-8466-794ed4572df4 #磁盘类型 SOURCE=image #存储位置 DEST=volume #存储大小 VOLUME_SIZE=20 #虚拟机名称 NAME=gsd-vm3 UUID= CREATE_TIME= NODE= NOVNC= VM_STATUS= create() { UUID=`nova boot --flavor $FLAVOR --nic net-id=$NET_ID --block-device source=$SOURCE,id=$IMG_ID,dest=$DEST,size=$VOLUME_SIZE,shutdown=preserve,bootindex=0 $NAME |grep -w ‘id‘|awk ‘{print $4}‘` if [ $? -ne 0 ]; then echo "the instance create fail" exit else echo "the instance create success" fi } info() { CREATE_TIME=`nova show $UUID|grep -w "created"|awk ‘{print $4}‘` NODE=`nova show $UUID|grep -w "OS-EXT-SRV-ATTR:host"|awk ‘{print $4}‘` NOVNC=`nova get-vnc-console $UUID novnc |grep ‘http‘|awk ‘{print $4}‘` printf "%-40s%-15s%-25s%-10s%s\n" "$UUID" "$NAME" "$CREATE_TIME" "$NODE" "$NOVNC" >> $LOG_FILE } create echo "Please wait...." sleep 20s info echo "DONE"
原文地址:https://www.cnblogs.com/yueshang/p/9786170.html
时间: 2024-10-10 01:01:41