Score:2

Bash execution is not working with one liner, how to fix that?

us flag

I have a bash file, which I kept remotely and below steps are working.

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

But, if I change that to one line command, it is not working/showing any output.

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

The actual code is:


#!/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

Please suggest.

Score:3
cn flag

Your first command, the one that works, uses curl's -L option which is (from 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. [. . .]

The way your URL works seems to be through that kind of redirection since without the -L option, you get nothing:

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

But with it, you get output:

$ 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

So all you need to do is use the -L option and then pass it to sudo bash (since you need to run it as root):

curl -L "$url"  | sudo bash

This is very risky, of course, and only run things like this if you are 100% the script is safe. Also, it seems weird that you are checking for the presence of curl in the script if you are using curl to download it. You already know curl is available. In any case, although you are testing for curl, your script actually uses wget instead. There are various other improvements you can make (backticks are deprecated, use var=$(command) instead of var=`command`; there are better ways of checking the distribution since yum can be installed on non RedHat based distros and apt on non-Debian ones), but those are off topic here.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.