Shell writes CGI script

Source: Internet
Author: User
Tags control characters diff echo command mkdir
29th Chapter CGI Script
This chapter contains content:
Basic CGI Script
Using server-side embedding (server Side Includes,ssi)
Get method
Post method
Creating interactive scripts
Ability to automatically reload Web pages with CGI scripts
CGI is a specification that textbook how to obtain information from the server or write information to the server. This script or CGI
Textbook can be implemented in any language. The most popular is the Perl language, but you will find that it can also be implemented with normal shell scripts.
All CGI scripts should be in the Cgi-bin directory of the Web server, but the directory may not be available on different servers. Can pass
View the Scriptalias section of the configuration file srm.conf to change the location of the directory and allow the server to run CGI scripts. All the textbook
Filenames should be suffixed with. CGI. Other Web pages are located in the HTML Htdocs directory with an. html suffix.
29.1 CGI Script instance:
$ VI firstpage.cgi
$!/bin/bash
# firstpage.cgi
echo "Content-type:text/html"
echo ""
echo ""
echo "This is MY first CGI page"
echo ""
echo "Stand-by to Stand-to!"
echo ""
The first line represents the path of the shell interpreter. The first echo command line tells the server that this is a MIME header, and the second line of the echo command line is used to
Displays a blank line. If there is no blank line behind the MIME header, the output of the CGI script will not display correctly.
29.2 Show shell command output

Add a shell command to the script so that the output of the command can be displayed in the browser.
For example, displays the number of users currently logged in, which can be achieved by passing the output of the WHO command to the WC command. and displays the current date.
$ VI pagetwo.cgi
#!/bin/bash
#pagetwo. CGI
#display a page using the output from a UNIX command
Mydate= ' Date +%a ' "%d" "%B" "%Y"
Users= ' Who |wc-l '
echo "Content-type:text/html"
echo ""
echo ""
echo "This is MY SECOND CGI page"
echo ""
echo "$MYDATE"
echo "Total amount of users on To-day is: $USERS"
echo ""
If ["$USERS"-lt 10]; Then
echo "It must be early or it's dinner time"
echo "Beacuse there ain ' s many users logged on"
Fi
echo ""
echo ""
29.3 using SSI
In order to embed CGI scripts, you must use server-side embedding (SSI) so that when a page is displayed, it will replace the SSI command with the corresponding command
Or the output of the script.
The configuration on Apache is:
Addhandler server-passed.shtml
AddType text/htm shtml
The 30th chapter of the Common shell command
1.basename
Format: basename path
The base command can separate file names from the path, typically used in shell scripts, for example:
$ BASENAME/HOME/SMALLFISH/IFCP
Ifcp
2.cat
Format: Cat options files
-V: Display control characters
Cat is the most commonly used text file display command
$ Cat MyFile
File Merge:
$ cat MyFile Myfile2 >>hold_file
3.cp
Format: CP options File1 file2
Options:
-I: Prompt the user before overwriting the file, confirmed by the user.
-P: Preserve permission mode and change time.
-R: Copies the corresponding directories and their subdirectories.
4.diff
Format: diff options File1 File2
Options:
-C: output in standard format
-I: Ignore case
5.dirname
Format: DirName pathname
The directory is exactly the opposite of basename, which returns the path section:
$ DIRNAME/HOME/SMALLFISH/IFCP
/home/smallfish
6.du
Format: Du options directory
Options:
-A: Displays the size of each file, not just the space occupied by the entire directory
-S: Show Totals only
Du shows that disk space consumption is represented by a block of 512 bytes. It is primarily used to display the space occupied by the directory.
Du-s.
5611.
7.file
Format: File filename
This command is used to determine the file type:
$ file IFCP
Ifcp:bourne-again Shell Script Text executable
$ file Mbox
MBOX:ASCII Mail Text
8.fuser
Format: Fuser Options File
Options:
-K: Kills all processes that access the file or file system
-U: Displays all processes that access the file or file system
The Fuser command can display all processes that access a file or file system. On some systems, the-U and-M options are interchangeable. You can also
The Fuser command is used in the statement.
List all active processes on/DEV/HDA8:
$ fuser-m/dev/hda8
/dev/hda8:2043e 2081m 2114e 2267e 2343e 2376e 2396e 2420e 2431e 2441e 2500e
2519e 2529e 2543e 2554e 2561 2561m 2955 2955c 2955e 3183e 3185e 3186m 3430e
11786m 11787m 11960m
9.head
Format: Head-number files
The head command can display the first 10 lines of the corresponding file. If you want to specify the number of rows to display, you can use the-number option.
Display the first line of the trap2.sh file:
$ head-1 trap2.sh
#!/bin/bash
10.logname
Format: logname
Displays the login user name currently in use:
$ logname
Smallfish
11.mkdir
Format: mkdir Options Directory
Options:
-M: Sets the access permissions when the directory is created by the value of this option.
More.
Format: More Options files
This command is similar to the page and PG commands, and can display the contents of a file on a split screen.
Options:
-C: Do not roll the screen, but overwrite to change the page
-D: Show hints at pagination
-N: Each screen displays
13.nl
Format: NL Options File
Options:
-I: line number increases by N, default is 1
-P: Do not re-count on a new page
The NL command can be used to column line numbers in a file, which is useful when printing source code or log files.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.