写过的bash比较少,经常遗忘,记录之
opensuse复制系统盘后,修改/boot/grub2/device.map,/etc/fstab,correct /etc/default/grub,/boot/grub2/grub.cfg的相关盘符信息
#/bin/bash
echo ------------------comfirm system disk-----------------
df
echo ------------------
RealSystemDisk=`df | awk ' $6=="/" {print $1} ' `
RealSystemDisk2=`expr $RealSystemDisk : '.*\(sd[^0-9]*\)'`
echo $RealSystemDisk
echo $RealSystemDisk2
echo ------------------comfirm system disk end-----------------
echo
echo
echo ------------------comfirm system disk id-----------------
ls -l /dev/disk/by-id/
echo ------------------
RealSystemDisk3=`ls -l /dev/disk/by-id/ | awk -v VV1="ata-.*"$RealSystemDisk2"$" ' $0 ~ VV1 { print $9}'`
echo $RealSystemDisk3
echo ------------------comfirm system disk id end-----------------
echo
echo
echo ------------------correct /boot/grub2/device.map-----------------
cat /boot/grub2/device.map
if [ ! -f /boot/grub2/bak.device.map ]; then
echo backup /boot/grub2/device.map
cp /boot/grub2/device.map /boot/grub2/bak.device.map
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*/\/dev\/disk\/by-id\/$RealSystemDisk3/g" /boot/grub2/device.map
echo ------------------correct /boot/grub2/device.map end-----------------
echo
echo
echo ------------------correct /etc/fstab-----------------
cat /etc/fstab
if [ ! -f /etc/bak.fstab ]; then
echo backup /etc/fstab
cp /etc/fstab /etc/bak.fstab
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*-part/\/dev\/disk\/by-id\/$RealSystemDisk3-part/g" /etc/fstab
echo ------------------correct /etc/fstab end-----------------
echo
echo
echo ------------------correct /etc/default/grub-----------------
cat /etc/default/grub
if [ ! -f /etc/default/bak.grub ]; then
echo backup /etc/default/grub
cp /etc/default/grub /etc/default/bak.grub
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*-part/\/dev\/disk\/by-id\/$RealSystemDisk3-part/g" /etc/default/grub
echo ------------------correct /etc/fstab end-----------------
echo
echo
echo ------------------grub2-mkconfig -o /boot/grub2/grub.cfg-----------------
grub2-mkconfig -o /boot/grub2/grub.cfg
echo ------------------grub2-mkconfig -o /boot/grub2/grub.cfg end-----------------
带参数版,要输入一个盘符
#/bin/bash
echo ------------------comfirm system disk-----------------
df
echo ------------------
RealSystemDisk=`df | awk ' $6=="/" {print $1} ' `
RealSystemDisk2=$1
echo RealSystemDisk $RealSystemDisk
echo RealSystemDisk2 $RealSystemDisk2
echo ------------------comfirm system disk end-----------------
echo
echo
echo ------------------comfirm system disk id-----------------
ls -l /dev/disk/by-id/
echo ------------------
#ls -l /dev/disk/by-id/ | awk -v VV1="ata-.*"$RealSystemDisk2"$" ' $0 ~ VV1 { print $9, VV1, NR}'
RealSystemDisk3=`ls -l /dev/disk/by-id/ | awk -v VV1="ata-.*"$RealSystemDisk2"$" ' $0 ~ VV1 { print $9}'`
echo RealSystemDisk3 $RealSystemDisk3
echo ------------------comfirm system disk id end-----------------
echo
echo
echo ------------------correct /mnt/boot/grub2/device.map-----------------
cat /mnt/boot/grub2/device.map
if [ ! -f /mnt/boot/grub2/bak.device.map ]; then
echo backup /mnt/boot/grub2/device.map
cp /mnt/boot/grub2/device.map /mnt/boot/grub2/bak.device.map
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*/\/dev\/disk\/by-id\/$RealSystemDisk3/g" /mnt/boot/grub2/device.map
echo ------------------correct /mnt/boot/grub2/device.map end-----------------
echo
echo
echo ------------------correct /mnt/etc/fstab-----------------
cat /mnt/etc/fstab
if [ ! -f /mnt/etc/bak.fstab ]; then
echo backup /mnt/etc/fstab
cp /mnt/etc/fstab /mnt/etc/bak.fstab
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*-part/\/dev\/disk\/by-id\/$RealSystemDisk3-part/g" /mnt/etc/fstab
echo ------------------correct /mnt/etc/fstab end-----------------
echo
echo
echo ------------------correct /mnt/etc/default/grub-----------------
cat /mnt/etc/default/grub
if [ ! -f /mnt/etc/default/bak.grub ]; then
echo backup /mnt/etc/default/grub
cp /mnt/etc/default/grub /mnt/etc/default/bak.grub
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*-part/\/dev\/disk\/by-id\/$RealSystemDisk3-part/g" /mnt/etc/default/grub
echo ------------------correct /etc/default/grub end-----------------
echo
echo
echo ------------------correct /mnt/boot/grub2/grub.cfg-----------------
cat /mnt/boot/grub2/grub.cfg
if [ ! -f /mnt/boot/grub2/bak.grub.cfg ]; then
echo backup /mnt/boot/grub2/grub.cfg
cp /mnt/boot/grub2/grub.cfg /mnt/boot/grub2/bak.grub.cfg
fi
echo -----------------
sed -i "s/\/dev\/disk\/by-id\/.*-part/\/dev\/disk\/by-id\/$RealSystemDisk3-part/g" /mnt/boot/grub2/grub.cfg
echo ------------------correct /mnt/boot/grub2/grub.cfg end-----------------
echo
echo
linux-q5zb:/dev/disk/by-id # ls -l
total 0
lrwxrwxrwx 1 root root 9 Sep 28 11:51 ata-ST3160812AS_5LSGEEQ6 -> ../../sda
lrwxrwxrwx 1 root root 10 Sep 28 11:51 ata-ST3160812AS_5LSGEEQ6-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Sep 28 11:51 ata-ST3160812AS_5LSGEEQ6-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Sep 28 11:51 ata-ST3160812AS_5LSGEEQ6-part3 -> ../../sda3
lrwxrwxrwx 1 root root 9 Sep 28 11:51 ata-ST3250318AS_6VM4DHS6 -> ../../sdb
lrwxrwxrwx 1 root root 9 Sep 28 11:51 scsi-SATA_ST3160812AS_5LSGEEQ6 -> ../../sda
lrwxrwxrwx 1 root root 10 Sep 28 11:51 scsi-SATA_ST3160812AS_5LSGEEQ6-part1 -> ../../sda1
lrwxrwxrwx 1 root root 10 Sep 28 11:51 scsi-SATA_ST3160812AS_5LSGEEQ6-part2 -> ../../sda2
lrwxrwxrwx 1 root root 10 Sep 28 11:51 scsi-SATA_ST3160812AS_5LSGEEQ6-part3 -> ../../sda3
lrwxrwxrwx 1 root root 9 Sep 28 11:51 scsi-SATA_ST3250318AS_6VM4DHS6 -> ../../sdb
lrwxrwxrwx 1 root root 9 Sep 28 11:51 wwn-0x5000c500187e82c1 -> ../../sdb
`ls -l /dev/disk/by-id/ | awk -v VV1="ata-.*"$RealSystemDisk2"$" ' $0 ~ VV1 { print $9}'`
遍历/dev/disk/by-id/下的盘符
-v VV1="ata-.*"$RealSystemDisk2"$" ,"ata-.*sda$" $在正则里表示结束位置
在awk里创建一个变量名为VV1的变量,这里的这个变量被用作了匹配字符串$0 ~ VV1,这里的$0是整个一行字符串
linux-rswj:/boot/grub2 # cat device.map
(hd0) /dev/disk/by-id/ata-GLOWAY_VAL16GS2-mSATA_DA5B17D0000030000238
(hd1) /dev/disk/by-id/usb-Kingston_DT_101_G2_20130306230641B00000116E-0:0
sed -i "s/\/dev\/disk\/by-id\/.*/\/dev\/disk\/by-id\/$RealSystemDisk3/g" /boot/grub2/device.map
-i表示在源文件上操作
"s/\/dev\/disk\/by-id\/.*/\/dev\/disk\/by-id\/$RealSystemDisk3/g" 是替换的正则规则,类似于
"s/\/dev\/disk\/by-id\/.*/\/dev\/disk\/by-id\/ata-GLOWAY_VAL16GS2-mSATA_DA5B17D0000030000238/g"
s表示替换,g表示一整行上的所有替换
\/是转义,代表/