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

おすすめ記事