using curl to receive result from .sh - linux

I am trying to get the results (CPU Temperature remote machine) from a .sh file using curl, Instead I receive the code from the .sh.
my current code
curl -s http://192.168.1.5/cgi-bin/temp.sh
instead of returning the temp it returns the .sh file code
I am running Apache2, and if I place the sh in a index.php file, I can grep the output, but that seems like a step too much.
Thanks for any advice.
Tried changing file to .cgi instead. Same results

I found changing my curl command to the following resolved the issue:
curl -s http://192.168.1.5/cgi-bin/temp.sh | sh
I just pipe my curl command to sh to return the result.

Related

How to solve bash error "syntax error at line 3: 'CYBER_UNAME=$' unexpected"?

This error happens when I run a software containing bash script with beggining like this:
#! /bin/sh
CYBER_UNAME=$(uname)
CYBER_UNAME_M=$(uname -m)
I tried to execute these two commands in terminal and it works fine. This error only happens when I run the shell script. What should I do?
The result of 'uname' is SunOS. This shell script cannot be modified since it's protected on our server.
The line
#! /bin/sh
should read:
#!/bin/bash
So, that script will probably never really work.
If you cannot modify the script in situ, you might want to copy it to your local directory and correct it.
Otherwise,
tail +2 scriptname|/bin/bash
might work.

How to use curl call a php to get file url first and then download?

On my webserver, the PHP code can get the latest file list.
On another Linux server, I want to use CRON and cURL to call this PHP code to get the file list first, and then download the file.
curl http://www.website.com/index.php
This code works and its results are string URL. (eg: http://www.website.com/files/new.zip )
However, I don't know how to post this string to curl for downloading this files. What I am tring to do is like this:
curl -O (curl http://www.website.com/index.php )
How can I make this work? Thanks.
You was almost there:
curl $(curl "http://www.website.com/index.php")
If you want to hide progress:
curl $(curl -s "http://www.website.com/index.php")
Looks like OP had line breaks/caret returns in outputed URL so http://.... became .ttp://....

"cannot execute binary file" when trying to run a shell script on linux

I am very new to linux and shell scriprting.
I am trying to run a shellscript from secure shell (ssh) on linux using following commands:
chmod +x path/to/mynewshell.sh
sh path/to/mynewshell.sh
I get this error:
path/to/mynewshell.sh: path/to/mynewshell.sh: cannot execute binary file.
Tried using this command:
bash path/to/mynewshell.sh
I get the same error.
Tried with this command: su - myusername sh path/to/mynewshell.sh
It is asking for my password and giving me this error: no such file or directory.
1.The result of cat -v path/to/mynewshell.sh is:
^#^#^#^#^#^#^#^#Rscript "$dir"/diver_script.R
done
2.When tried 'less path/to/mynewshell.sh' i got this on my terminal:
#!/bin/bash/Rscript^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
for dir in /path/to/* ; do
^#^#^#^#^#^#^#^#Rscript "$dir"/myRscript.R
done
3.When i ran file path/to/mynewshell.sh : i got this "Bourne-Again shell script text executable"
Please give any advice on how I can try executing the shellscript.
chmod -x removes execution permission from a file. Do this:
chmod +x path/to/mynewshell.sh
And run it with
/path/to/mynewshell.sh
As the error report says, you script is not actually a script, it's a binary file.
I was getting the same error running my shell script through a bash interpreter in PowerShell. I ran dos2unix myscript.sh on the shell script, and now it runs ok.
From a proposed duplicate:
run_me.sh.xz: run_me.sh.xz: cannot execute binary file
This is because the file is compressed, as indicated by the .xz extension. You need to remove the compression before the file can be used.
xz -d ./run_me.sh.xz
chmod +x ./run_me.sh # probably not necessary if you already did that before
./run_me.sh
Other compression schemes like gzip (.gz extension), bzip2 (.bz2 extension) etc behave similarly; you just have to know the name of the command to uncompress it, which is of course usually easy to google.
To anyone else having the problem i had.
i was trying to run a 16 bit unicode text file converted to a shell script, this doesn't work as all 16 bit unicode text files have a 0xFFFE marker at the start making mac os not like the file and this gives the “cannot execute binary file” error.
open the text file click on "Format" at the top, go down to "Make Plain Text" click it.
open your terminal type chmod 777 /path/to/file.sh
put in terminal: /path/to/file.sh to run it
That script is simply not a shell script.
A shell script is usually readable and contains shell code.
The output your cat command shows looks indeed like it's a binary of some sort.
As some note, it might be because of a file conversion issue when copying but it looks more like an actual binary to me.
You can check what it is identified as with the file command so:
file path/to/mynewshell.sh
Just start with a clean script and rewrite the code, it looks like you just want to run some R scripts in a directory?
Make sure the R scripts point to the right R script executioner.
In my case I had a bash script that would not execute. The file was originally generated from a find ... -print0 command. Leaving a \0 character the script, removing that character solved my problem.

sh script with scp variables

I am trying to writing a simple script which will figure out the latest version of a file by the filename and then download that file to the local computer.
What I cant figure out is why my code will work in the shell, but not work when I run it as a script. I am also running my script on cygwin, not sure if that will make a difference.
Here is the script
#!/bin/sh
x=$(ssh user#hostname 'ls -r -t /vgf/day1*.gif | tail -1')
echo $x
scp user#hostname:"${x}" /images/day1.gif
x is correctly assigned, but when I get to the scp command I receive something along the lines of
: No such file or directoryif
However if I run the scp command in the shell it will work
$ sh download.sh
/vgf/day1.gif
: No such file or directoryif
$ scp user#hostname:"${x}" /images/day1.gif
day1.gif 100% 22KB 22.1KB/s 00:00
I would be open to different solutions. If I could prevent the version increasing via some linux administration, I may follow that route, although I am still wondering what the problem is here.
By version, I mean day1_001.gif and the new version becoming day1_002.gif and so on. So when the file saves a file day1.gif, it will overwrite the original without creating another version.

shell script wget not working when used as a cron job

i have a function in a php web app that needs to get periodically called by a cron job. originally i just did a simple wget to the url to call the function and everything worked fine, but ever since we added user auth i am having trouble getting it to work.
if i manually execute these commands i can login, get the cookie and then access the correct url:
site=http://some.site/login/in/here
cookie=`wget --post-data 'username=testuser&password=testpassword' $site -q -S -O /dev/null 2>&1 | awk '/Set-Cookie/{print $2}' | awk 'NR==2{print}'`
wget -O /dev/null --header="Cookie: $cookie" http://some.site/call/this/function
but when executed as a script, either manually or through cron, it doesn't work.
i am new to shell scripting, any help would be appreciated
this is being run on ubuntu server 10.04
OK simple things first -
I assume the file begins with #!/bin/bash or something
You have chmodded the file +x
You're using unix 0x0d line endings
And you're not expecting to return any of the variables to the calling shell, I presume?
Failing this try teeing the output of each command to a log file.
In theory, the only difference from manually executing these and using a script would be the timing.
Try inserting a sleep 5 or so before the last command. Maybe the http server does some internal communication and that takes a while. Hard to say, because you didn't post the error you get.

Resources