How to convert my /bin/sh script with cgi and html to run it on browser!??


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to convert my /bin/sh script with cgi and html to run it on browser!??
# 1  
Old 10-02-2019
How to convert my /bin/sh script with cgi and html to run it on browser!??

Hello,

I want to run this script on my CentOS 6 via browser :
________________________________________________________________________________________________

Code:
#!/bin/sh
echo Username?
read MY_NAME
echo Provisional file name?
read MY_FILE
echo File NAME you want to save?
read MY_FILE2
cat /opt/SUNWappserver/nodeagents/ins1/logs/* | grep $MY_NAME > $MY_FILE.txt
sed 's/INFO.*;|//g' $MY_FILE.txt > $MY_FILE2.txt

________________________________________________________________________________________________


For that I have created 2 files : /var/www/html/test.html and /var/www/cgi-bin/script.sh:

1. I have created one file test.html
________________________________________________________________________________________________

Code:
<HTML>
<TITLE>LOGS</TITLE>
<BODY>
<b>LOGs Instacne</b><p>
<form action="cgi-bin/script.sh" method="post">
<br>
<input type="submit" value="LOG Button">
</form>
</BODY>
</HTML>

2. And /var/ww/cgi-bin/script.sh
Code:
#!/bin/sh -f
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Inscance LOGs</title>'

#Step 1
echo "<form method=GET action=\"${SCRIPT}\">"\
     '<table nowrap>'\
         '<tr><td>UserName</TD><TD><input type="text" name="val_x" size=12></td></tr>'\
         '</tr></table>'
#Step 2
echo "<form method=GET action=\"${SCRIPT}\">"\
        '<table nowrap>'\
                 '<tr><td>File Name not cleaned?</TD><TD><input type="text" name="val_y" size=12></td></tr>'\
                '</tr></table>'
#Step 3
echo "<form method=GET action=\"${SCRIPT}\">"\
        '<table nowrap>'\
                 '<tr><td>File NAME cleaned</TD><TD><input type="text" name="val_a" size=12></td></tr>'\
                '</tr></table>'

echo '<br><input type="submit" value="Search">'\
       '<input type="reset" value="Reset"></form>'

  if [ -z "$QUERY_STRING" ]; then
        exit 0
  else
     XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     YY=`echo "$QUERY_STRING" | sed -n 's/^.*val_y=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     AA=`echo "$QUERY_STRING" | sed -n 's/^.*val_a=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     echo "UserName: " $XX
     echo '<br>'

     echo "File Not_Cleaned: " $YY
     echo '<br>'

     echo "File Cleaned: " $AA
     echo '<br>'


echo "cat /opt/SUNWappserver/nodeagents/ins1/logs/*" | grep $XX > $YY.txt

          browser="`echo $XX | cut -d\  -f1`"
          echo '<br>'
          echo "Browser: " $browser

BB="`echo $browser | sed 's/INFO.*;|//g' $YY.txt > $AA.txt`"
     echo '<br>'
     echo '<br>'
     echo "Result: " $BB
     echo '<br>'
fi
echo '</body>'
echo '</html>'
exit 0

________________________________________________________________________________________________


When I open in browser: localhost/test.html click on button "LOG Button" it redirects me on localhost/cgi-bin/script.sh.
I fill in the required fields and click on the search button to show the data, but the cat dhe sed command is not executed (echo "cat /opt/SUNWappserver/nodeagents/ins1/logs/*" | grep $XX > $YY.txt) and (sed 's/INFO.*;|//g' $YY.txt > $AA.txt).


Can you give me any suggestions to execute the script shell above in the browser and if i have chosen the wrong way how can i execute this script?

Regards,
Juta2020

Last edited by vgersh99; 10-02-2019 at 10:18 AM.. Reason: code tags, please!
# 2  
Old 10-02-2019
One important thing to keep in mind with CGI. It doesn't run as you, it runs as the web server's user. Which means a CGI script doesn't -- AND SHOULDN'T! -- have permissions to write to the current folder, whatever it happens to be. You should create a file in a known writable location, like /tmp/$$-query.txt or some such. $$ is the script's PID, which is a quick and dirty way of getting a unique file name. (Once you have stuff working, see mktemp for a fancier, safer option.) And yes, you'll need to delete the file when you're done!

What's 'echo cat' for? If you want to cat, run cat. But you don't actually need cat here, grep (and most other commands) is fully capable of reading files without cat's help.

Also, you should be putting variables inside double quotes, and not putting * inside double quotes.

Code:
# Catch error messages so you see them in the web browser
exec 2>&1
# Check log files for 
grep "$XX" /opt/SUNWappserver/nodeagents/ins1/logs/* > /tmp/$$.txt 

...

rm /tmp/$$.txt

Lastly, does your web server have read-access to /opt/SUNWappserver/nodeagents/ins1/logs/* ? It'll need it.
# 3  
Old 10-02-2019
Also, a quick and simple way to handle QUERY_STRING in bash is:

Code:
OLDIFS="$IFS"
IFS="&=" # Split strings upon ampersand and equals
# If query_string="a=b&c=d, this sets $1=a, $2=b, $3=c, $4=d
set -- ${QUERY_STRING}
IFS="$OLDIFS" # Set splitting back to normal

while [ "$#" -gt 0 ]
do
        case "$1" in
        val_x) XX="$2" ; shift ;;
        val_y) YY="$2" ; shift ;;
        val_z) ZZ="$2" ; shift ;;
        *) ;; # Ignore unknown option
        esac
        shift # Throw away $1, move $2 etc down
done

Faster than running sed three times.

There's lots of stuff that's not being handled by using bash, like %20 encoding for spaces, etc.
# 4  
Old 10-03-2019
Hello Corona688.

I have added both your suggestion on my file script.sh:

____________________________________________________________________________________________________ ____
Code:
#!/bin/sh -f
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'
echo '<title>Inscance LOGs</title>'

#Step 1
echo "<form method=GET action=\"${SCRIPT}\">"\
     '<table nowrap>'\
         '<tr><td>UserName: </TD><TD><input type="text" name="val_x" size=12></td></tr>'\
         '</tr></table>'
#Step 2
echo "<form method=GET action=\"${SCRIPT}\">"\
        '<table nowrap>'\
                 '<tr><td>File Name not cleaned: </TD><TD><input type="text" name="val_y" size=12></td></tr>'\
                '</tr></table>'
#Step 3
echo "<form method=GET action=\"${SCRIPT}\">"\
        '<table nowrap>'\
                 '<tr><td>File NAME cleaned: </TD><TD><input type="text" name="val_a" size=12></td></tr>'\
                '</tr></table>'

echo '<br><input type="submit" value="Search">'\
       '<input type="reset" value="Reset"></form>'

  if [ -z "$QUERY_STRING" ]; then
        exit 0
  else
     XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     YY=`echo "$QUERY_STRING" | sed -n 's/^.*val_y=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     AA=`echo "$QUERY_STRING" | sed -n 's/^.*val_a=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`
     echo "UserName: " $XX
     echo '<br>'

     echo "File Not_Cleaned: " $YY
     echo '<br>'

     echo "File Cleaned: " $AA
     echo '<br>'

# Catch error messages so you see them in the web browser
exec 2>&1
# Check log files for
grep "$XX" /var/www/html/** > /tmp/$$.txt

rm /tmp/$$.txt


OLDIFS="$IFS"
IFS="&=" # Split strings upon ampersand and equals
# If query_string="a=b&c=d, this sets $1=a, $2=b, $3=c, $4=d
set -- ${QUERY_STRING}
IFS="$OLDIFS" # Set splitting back to normal

while [ "$#" -gt 0 ]
do
        case "$1" in
        val_x) XX="$2" ; shift ;;
        val_y) YY="$2" ; shift ;;
        val_z) ZZ="$2" ; shift ;;
        *) ;; # Ignore unknown option
        esac
        shift # Throw away $1, move $2 etc down
done

fi
echo '</body>'
echo '</html>'
exit 0

____________________________________________________________________________________________________ ____


But, on browser I receive this error message:

Code:
UserNaname: juta
File Name not cleaned:  juta_not_clean  	
File NAME cleaned: juta_ok	

UserName: juta
File Not_Cleaned: juta_not_clean
File Cleaned: juta_ok
grep: /var/www/html/**: No such file or directory

How can I solve this ???
So , With my script I want through web browser "to find files that contain a text string" for example on /var/www/html/* filling in the fields: username, File Name not cleaned, File NAME cleaned to take results and save results in file.txt after that do this command:
Code:
sed 's/INFO.*;|//g' $YY.txt > $AA.txt

, in this case $YY.txt it is file.txt.
I want to use sed to clean to clear unnecessary information through the rows that will appear, a command that works fine when running it through the terminal in the script below:


Code:
#!/bin/sh
echo Username?
read MY_NAME
echo Provisional file name?
read MY_FILE
echo File NAME you want to save?
read MY_FILE2
cat /opt/SUNWappserver/nodeagents/ins1/logs/* | grep $MY_NAME > $MY_FILE.txt
sed 's/INFO.*;|//g' $MY_FILE.txt > $MY_FILE2.txt

This sed command is important for me!

So, can you help me with any script to run through the web browser and perform the above functions?

Best Regards,
Juta2020

Last edited by Scrutinizer; 10-03-2019 at 04:46 PM.. Reason: CODE tags
# 5  
Old 10-04-2019
Quote:
Originally Posted by juta2020
Hello Corona688.

I have added both your suggestion on my file script.sh
You missed the point. set and case is a replacement for XX=`echo "$QUERY_STRING" | sed -n 's/^.*val_x=\([^&]*\).*$/\1/p' | sed "s/%20/ /g"`

As for "no such file or directory", I'll hazard a wild guess and say there's no such file or directory. You want one *, not two.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 10-07-2019
Ok. Thank you Corona688! BUt I have anothere question. After I will grep and save file grep -r "$XX" /var/www/html/ > /tmp/$YY.txt how can I sed it with command :

Code:
sed 's/INFO.*;|//g' $YY.txt > $ZZ.txt            

??

Best Regards,
Jurgen

Moderator's Comments:
Mod Comment
Use code tags please

Last edited by juta2020; 10-07-2019 at 08:02 AM..
# 7  
Old 10-07-2019
The same as before, except you must write files inside /tmp/ instead of the current directory.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I use my /bin/sh script on web browser?

Hello, I have created my script which works properly through the terminal, but I want to convert it to perform all functions as it performs through terminal, but in this case perform through web browser. My /bin/sh script is:... (1 Reply)
Discussion started by: juta2020
1 Replies

2. Shell Programming and Scripting

Run command through html+cgi in bash

Hi everyone, I want to kill process through the web, so I create html page with single bottom that run kill command in shell script with CGI. Here is html code: <td><form METHOD="GET" action="http://IP:port/cgi_bin/script.cgi" > <input type="submit" value= "Submit" > <INPUT name="q"... (7 Replies)
Discussion started by: indeed_1
7 Replies

3. UNIX for Beginners Questions & Answers

How to run script through CGI?

Hi I have written a script and I want it to be run from web with the help of CGI. can you please guide me . below script is working fine if run from backend but not sure how I should run through web. #!/bin/bash string1=look string2=0 string3=.sdn.dnb echo -n "enter... (3 Replies)
Discussion started by: scriptor
3 Replies

4. Shell Programming and Scripting

Perl cgi pages out of cgi-bin folder in WINDOWS

Hi team, I have a typical problem with cgi pages in apache webserver in WINDOWS I am able to execute(display) the pages that are saved in cgi-bin folder. But I am not able to execute the pages stored in htdocs or other folder other than cgi-bin folder. Could anyone please let me know how... (1 Reply)
Discussion started by: scriptscript
1 Replies

5. UNIX and Linux Applications

execute shell script using CGI for html site

hi there im currently in the process of creating a website for use basically within our org. im using a os x machine and installed MAMP - which includes Apache, mysql... the site will be used by techs to primarily install pkgs files onto os x devices. i would like to have buttons or hyperlinks... (2 Replies)
Discussion started by: sheshe
2 Replies

6. Shell Programming and Scripting

How to pass data from server (CGI script) to client (html page)

Hi I know how to pass data from client side (html file) to server using CGI script (POST method). I also know how to re-create the html page from server side after receiving the data (using printf). However I want to write static pages on client side (only the structure), and only to pass... (0 Replies)
Discussion started by: naamabm
0 Replies

7. Shell Programming and Scripting

cgi script to echo a html file

Hi, I'm learning some simple cgi scripting. I can make a script like this, so my browser shows "Hello World" /www/cgi-bin/name.sh --- #!/bin/sh MyName=World echo "<html> Hello $MyName </html>" --- What I'd like is to have a separate html and script files in the cgi folder so ... (1 Reply)
Discussion started by: Performer
1 Replies

8. Shell Programming and Scripting

running a cgi script even when browser is closed

hii, i have a cgi script file which may take some hours to complete. The script logs the output and mails the user. so the browser need not be open for the output. But currently the script dies off the instant the browser is closed or other pages are viewed. Is there a way out .. ? i have... (0 Replies)
Discussion started by: damn_bkb
0 Replies

9. Shell Programming and Scripting

html - df -k cgi script

Hey - I am new to cgi scripting... just writing a script to output df -k output to html page... but I cannot get the df lines on separate lines on the page, it all comes out on one line and is not very readable.. any suggestions? My script is below - please keep in mind I am only new to it so... (1 Reply)
Discussion started by: frustrated1
1 Replies

10. UNIX for Dummies Questions & Answers

Run ksh script from cgi

Hi, I'm developing a system which requires me to run a ksh script from within a cgi script. What sort of syntax will I need to do this, I'm sure it's simple but can't find out how anywhere! Thanks. (2 Replies)
Discussion started by: hodges
2 Replies
Login or Register to Ask a Question