Bash 실행이 하나의 라이너에서 작동하지 않습니다. 이를 해결하는 방법은 무엇입니까?

Bash 실행이 하나의 라이너에서 작동하지 않습니다. 이를 해결하는 방법은 무엇입니까?

원격으로 보관하고 아래 단계가 작동하는 bash 파일이 있습니다.

curl -fsSL -o check.sh https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh
chmod 700 check.sh
sudo ./check.sh

하지만 한 줄 명령으로 변경하면 작동하지 않거나 출력이 표시되지 않습니다.

curl https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh | bash

실제 코드는 다음과 같습니다:


#!/usr/bin/env bash
count_apt=`whereis apt`
count_yum=`whereis yum`
## echo ${#count_apt} ${#count_yum}
if (( ${#count_yum} > 4 )); then
 echo "---Redhat distributions are not supported---"
 exit 0
fi

if (( ${#count_apt} > 4 )); then
 echo "---Found Ubuntu Distribution---"
 echo "---Started installation of dependencies---"
 apt install -y curl
 wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/agentxw_1.036-1_amd64.deb
 apt install -y ./agentxw_1.036-1_amd64.deb
 wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/xvision_0.97-1_amd64.deb
 apt install -y ./xvision_0.97-1_amd64.deb
 echo "---Completed installation of dependencies---"
 echo "---Started installation of main package with its dependencies: xenowulf-ai---"
 wget https://github.com/uday1kiran/python_deb_create/raw/testdeb2/tmp/xenowulf-ai_1.0_all.deb
 apt install -y ./xenowulf-ai_1.0_all.deb
 echo "---Completed installation of main pacakge: xenowulf-ai---"
fi

제안해주세요.

답변1

작동하는 첫 번째 명령은 (from ) -L인 컬의 옵션을 사용합니다.man curl

   -L, --location
          (HTTP)  If the server reports that the requested page has moved
          to a different location (indicated with a Location: header  and
          a  3XX  response code), this option will make curl redo the re‐
          quest on the new place. [. . .]

귀하의 URL이 작동하는 방식은 옵션이 없으면 -L아무것도 얻지 못하기 때문에 그러한 종류의 리디렉션을 통한 것 같습니다.

$ url="https://github.com/uday1kiran/python_deb_create/raw/testbash/tmp/check.sh"
$ curl "$url"  2>/dev/null | wc
      0       0       0

하지만 이를 사용하면 다음과 같은 결과를 얻을 수 있습니다.

$ curl -L "$url" 2>/dev/null 
#!/usr/bin/env bash
count_apt=`whereis apt`
count_yum=`whereis yum`
## echo ${#count_apt} ${#count_yum}
if (( ${#count_yum} > 4 )); then
 echo "---Redhat distributions are not supported---"
 exit 0
fi

if (( ${#count_apt} > 4 )); then
 echo "---Found Debian Distribution---"
 echo "---Started installation of dependencies---"
 apt install -y curl
 wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/agentxw_1.036-1_amd64.deb
 apt install -y ./agentxw_1.036-1_amd64.deb
 wget https://xenowulf-deb.s3.us-west-2.amazonaws.com/xvision_0.97-1_amd64.deb
 apt install -y ./xvision_0.97-1_amd64.deb
 echo "---Completed installation of dependencies---"
 echo "---Started installation of main package with its dependencies: xenowulf-ai---"
 wget https://github.com/uday1kiran/python_deb_create/raw/testdeb2/tmp/xenowulf-ai_1.0_all.deb
 apt install -y ./xenowulf-ai_1.0_all.deb
 echo "---Completed installation of main pacakge: xenowulf-ai---"
fi

따라서 당신이 해야 할 일은 -L옵션을 사용한 다음 이를 전달하는 것뿐입니다 sudo bash(루트로 실행해야 하기 때문에).

curl -L "$url"  | sudo bash

물론 이것은 매우 위험하며 스크립트가 100% 안전할 경우에만 이와 같은 작업을 실행하십시오. 또한 다운로드하는 데 curl사용하는 경우 스크립트에 존재 여부를 확인하는 것이 이상해 보입니다 . curl당신은 이미 curl사용할 수 있다는 것을 알고 있습니다. 어쨌든 테스트 중이지만 curl스크립트는 실제로 wget대신 을 사용합니다. 다른 다양한 개선 사항이 있습니다(백틱은 더 이상 사용되지 않습니다. var=$(command)대신 사용합니다 var=`command`. RedHat 기반이 아닌 배포판과 Debian이 아닌 배포판에 yum을 설치할 수 있으므로 배포판을 확인하는 더 좋은 방법이 있습니다 apt). 하지만 여기서는 주제에서 벗어났습니다.

관련 정보