ShellCheck 个别重定向警告

ShellCheck 个别重定向警告

当我检查我的代码时,我正在使用此处文档创建启动脚本https://www.shellcheck.net它给了我一个警告,可以忽略它。但我想知道是否有更好的方法来编写代码并尽可能避免该警告。

https://github.com/koalaman/shellcheck/wiki/SC2129

cat << EOF >> /usr/local/etc/rc.d/"${name}"
#!/bin/sh
. /etc/rc.subr
cpu="${cpu}"
ram="${ram}"
tap="${tap}"
name="${name}"
EOF
  sed -n '3,16p' ./bhyve >>  /usr/local/etc/rc.d/"${name}"
  cat << EOF >> /usr/local/etc/rc.d/"${name}"
start_cmd=${name}_start
stop_cmd=${name}_stop
restart_cmd=${name}_restart
delete_cmd=${name}_delete
${name}_start() {
EOF
  cat << 'EOF' >> /usr/local/etc/rc.d/"${name}"
  while true; do
    bhyve -Hw -c "${cpu}" -m "${ram}"G \
      -s 0,hostbridge \
      -s 1,virtio-net,"${tap}" \
      -s 2,virtio-blk,/dev/zvol/zroot/VMs/"${name}"/disk0 \
      -s 29,fbuf,tcp=0.0.0.0:"${vnc}",w=1024,h=768 \
      -s 30,xhci,tablet \
      -s 31,lpc \
      -l bootrom,/zroot/VMs/efi.fd \
      "${name}" || break;
  done > /dev/null 2>&1 &
}
EOF
  cat << EOF >> /usr/local/etc/rc.d/"${name}"
${name}_stop() {
EOF
  cat << 'EOF' >> /usr/local/etc/rc.d/"${name}"
  bhyvectl --vm="${name}" --force-poweroff
}
EOF
  cat << EOF >> /usr/local/etc/rc.d/"${name}"
${name}_restart() {
EOF
cat << 'EOF' >> /usr/local/etc/rc.d/"${name}"
  bhyvectl --vm="${name}" --force-reset
}
EOF
  cat << EOF >> /usr/local/etc/rc.d/"${name}"
${name}_delete() {
EOF
  cat << 'EOF' >> /usr/local/etc/rc.d/"${name}"
  bhyvectl --vm="${name}" --destroy
  sleep 5
  ifconfig "${tap}" destroy
  sysrc cloned_interfaces-="${tap}"
EOF
  cat << EOF >> /usr/local/etc/rc.d/"${name}"
  sed -i '' 's/ addm ${tap}//g' /etc/rc.conf
  sed -i '' 's/service ${name} start || sleep 5//g' /usr/local/etc/rc.d/bhyve
  sed -i '' '/^$/d' /usr/local/etc/rc.d/bhyve
EOF
  cat << 'EOF' >> /usr/local/etc/rc.d/"${name}"
  zfs destroy -r zroot/VMs/"${name}"
  rm /usr/local/etc/rc.d/"${name}"
}

load_rc_config "${name}"
run_rc_command "$1"
EOF
  # Add the VM to /usr/local/etc/rc.d/bhyve for autostart
  sed -i '' -e "9s/^/service ${name} start || sleep 5\n/g"  /usr/local/etc/rc.d/bhyve

答案1

好吧,我咬一下。首先完全按照网站告诉您的操作,您会得到

{
cat << EOF
#!/bin/sh
. /etc/rc.subr
cpu="${cpu}"
ram="${ram}"
tap="${tap}"
name="${name}"
EOF
  sed -n '3,16p' ./bhyve
  cat << EOF
start_cmd=${name}_start
stop_cmd=${name}_stop
restart_cmd=${name}_restart
delete_cmd=${name}_delete
${name}_start() {
EOF
  cat << 'EOF'
  while true; do
    bhyve -Hw -c "${cpu}" -m "${ram}"G \
      -s 0,hostbridge \
      -s 1,virtio-net,"${tap}" \
      -s 2,virtio-blk,/dev/zvol/zroot/VMs/"${name}"/disk0 \
      -s 29,fbuf,tcp=0.0.0.0:"${vnc}",w=1024,h=768 \
      -s 30,xhci,tablet \
      -s 31,lpc \
      -l bootrom,/zroot/VMs/efi.fd \
      "${name}" || break;
  done > /dev/null 2>&1 &
}
EOF
  cat << EOF
${name}_stop() {
EOF
  cat << 'EOF'
  bhyvectl --vm="${name}" --force-poweroff
}
EOF
  cat << EOF
${name}_restart() {
EOF
cat << 'EOF'
  bhyvectl --vm="${name}" --force-reset
}
EOF
  cat << EOF
${name}_delete() {
EOF
  cat << 'EOF'
  bhyvectl --vm="${name}" --destroy
  sleep 5
  ifconfig "${tap}" destroy
  sysrc cloned_interfaces-="${tap}"
EOF
  cat << EOF
  sed -i '' 's/ addm ${tap}//g' /etc/rc.conf
  sed -i '' 's/service ${name} start || sleep 5//g' /usr/local/etc/rc.d/bhyve
  sed -i '' '/^$/d' /usr/local/etc/rc.d/bhyve
EOF
  cat << 'EOF'
  zfs destroy -r zroot/VMs/"${name}"
  rm /usr/local/etc/rc.d/"${name}"
}

load_rc_config "${name}"
run_rc_command "$1"
EOF
} > /usr/local/etc/rc.d/"${name}"

  # Add the VM to /usr/local/etc/rc.d/bhyve for autostart
  sed -i '' -e "9s/^/service ${name} start || sleep 5\n/g" /usr/local/etc/rc.d/bhyve

正如 @roaima 所建议的,此时您会对${name}内部'EOF'块所在的行感到非常非常怀疑。你可以逃脱它,因为你name="${name}"生成的文件的开头有一个 。此时,您将脚本更改为类似的内容,将${name}_start和其他函数名称更改为实际名称,以使程序在语法上正确,但在运行时之前保留其他变量未扩展。

# Start of redirection to rc.d file       
{                                            
# Header, with variables being expanded.
cat << CAT_EOF                                                                             
#!/bin/bash                                                                                  
. /etc/rc.subr                               
cpu="${cpu}"       
ram="${ram}"                                 
tap="${tap}"                                 
name="${name}"                               
CAT_EOF                                                                                    
                                                                                           
# some stuff you want to preserve, probably would be better to
# have some markers in the file rather than magic line numbers                    
sed -n '3,16p' ./bhyve 

# Main body, changing ${name} to the name and leaving the rest intact.
# Maybe might want to add to the variables you want to expand in the future
sed 's/${name}/'"${name}"'/g;s/^  //' << 'SED_EOF'
  start_cmd=${name}_start
  stop_cmd=${name}_stop
  restart_cmd=${name}_restart
  delete_cmd=${name}_delete
  ${name}_start() {
    while true; do
      bhyve -Hw -c "${cpu}" -m "${ram}"G \
        -s 0,hostbridge \
        -s 1,virtio-net,"${tap}" \
        -s 2,virtio-blk,/dev/zvol/zroot/VMs/"${name}"/disk0 \
   /usr/local/etc/rc.d/"${name}"     -s 29,fbuf,tcp=0.0.0.0:"${vnc}",w=1024,h=768 \
        -s 30,xhci,tablet \
        -s 31,lpc \
        -l bootrom,/zroot/VMs/efi.fd \
        "${name}" || break;
    done > /dev/null 2>&1 &
  }
  ${name}_stop() {
    bhyvectl --vm="${name}" --force-poweroff
  }
  ${name}_restart() {
    bhyvectl --vm="${name}" --force-reset
  }
  ${name}_delete() {
    bhyvectl --vm="${name}" --destroy
    sleep 5
    ifconfig "${tap}" destroy
    sysrc cloned_interfaces-="${tap}"
    sed -i '' "s/ addm ${tap}//g" /etc/rc.conf
    sed -i '' "s/service ${name} start || sleep 5//g" /usr/local/etc/rc.d/bhyve
    sed -i '' '/^$/d' /usr/local/etc/rc.d/bhyve
    zfs destroy -r zroot/VMs/"${name}"
    rm /usr/local/etc/rc.d/"${name}"
  }
  
  load_rc_config "${name}"
  run_rc_command "$1"
SED_EOF
} > /usr/local/etc/rc.d/"${name}"


# Add the VM to /usr/local/etc/rc.d/bhyve for autostart
sed -i '' -e "9s/^/service ${name} start || sleep 5\n/g" /usr/local/etc/rc.d/bhyve

相关内容