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

관련 정보