Your SlideShare is downloading. ×
Using Sed on A Moxa Linux Embedded Computer
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Using Sed on A Moxa Linux Embedded Computer

254
views

Published on

See More Embedded Computer White Papers by Visiting http://www.moxa.com/Resource/whitepapers.aspx

See More Embedded Computer White Papers by Visiting http://www.moxa.com/Resource/whitepapers.aspx

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
254
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Using sed on a Moxa Linux EmbeddedComputer: A CGI Exercisesed is a stream editor for filtering and transforming text in Linux. In theexample below, you can write a script with sed to find a matched pattern, Input,and replace with the substituted string Output. Input line will be changed Pattern: Input sd e Substitute: Output Output line will be changedFig. 1: sed is a stream editorIn this article we will take advantage of a shell script with the sed command towrite a small CGI (Common Gateway Interface) program to change thehostname. There are two files created in this exercise. One is an html webpage (hostname.htm) for the user to input a new host name, the other is theexecutable CGI program (hostname.cgi) to get the new host name andconfigure it on the system.Seting up the Web Page on a MoxaEmbedded ComputerIn this exercise, we will change the host name using the Apache web server.However, the default user of the web server “nobody” does not have thepermissions to change the host name. Hence, before starting up the Apacheweb server, you need to modify the configuration file/etc/apache/conf/httpd.conf to run the Apache server as root.
  • 2. Open the configuration file and find the attributes User and Group. Set thevalue of User and Group to root.Put the below html file in the default web directory /home/httpd/htdocs. Whenthe submit button is clicked, the web page will trigger the action to execute theCGI program hostname.cgi under the directory /home/httpd/cgi-bin.hostname.htm<HTML><HEAD><TITLE>Hostname configure page</TITLE></HEAD><BODY bgcolor="#cccccc" text="#000000"><H2>Hostname configure</H2><form method=get action="/cgi-bin/hostname.cgi"> <pre> Hostname: <input name="hostname" size=42> </pre> <input type="submit" value="Submit" size=20> <input type="reset" value="Clear" size=20></form></BODY></HTML>The web page should look like the screenshot below:
  • 3. Using the hostname.cgi ScriptWe use a bash shell script named hostname.cgi to get parameters from theweb server and use sed to find the specific input value of hostname in theenvironmental variable $QUERY_STRING. Note that you have to set theexecutable permission to hostname.cgi so that the Apache server can invoke itvia the request from hostname.htm. The contents of hostname.cgi are shownbelow:hostname.cgi#!/bin/shecho "Content-type: text/html"echo# our html codeecho "<html>"echo "<head><title>Host name configure</title></head>"echo "<body>"echo "<pre>"# print out the environment settings#/usr/bin/env
  • 4. hostname=`echo "$QUERY_STRING" | sed -ns/^.*hostname=([^&]*).*$/1/p`orig_hostname=`cat /etc/hostname`# Show the configuration on web pageecho "The original hostname $orig_hostname will be configuredas $hostname"# Change the hostname via /bin/hostname command/bin/hostname $hostnameecho "The new hostname is $hostname"echo "</pre>"echo "</body>"echo "</html>"In this script, we would like to explain how the sed command gets the userinput value from the web page. Related lines are shown in red color in theabove scripts.$QUERY_STRING is an environmental variable passed by Apache server tothe CGI program. The value of this variable might be:hostname=Moxa_newsed -n s/^.*hostname=([^&]*).*$/1/p:-n: It will not print anything unless an explicit request to print is found.s: The substitute command changes all occurrences of the regular expressioninto a new value./syntax/: Between the two forward slashes “/” is the regular expression.(^.*hostname=([^&]*).*$) means from the beginning of the variable“$QUERY_STRING”, find the pattern “hostname=” and go to the end of line.Useful reference sites for sed and regular expressions are listed at the end ofthis document.
  • 5. The following web page displays the results:NotesThe following Moxa Linux embedded computers support sed in the followingfirmware versions and later.Model name Firmware versionUC-7410 v2.1UC-7420 v2.1UC-7402 v2.1UC-7408 v2.1DA-660 v1.1DA-661-LX v1.3DA-662-LX v1.3DA-663-LX v1.3UC-7410-LX Plus v1.3UC-7420-LX Plus v1.3UC-7402-LX Plus v1.3UC-7408-LX Plus v1.3UC-8410-LX v1.0UC-8416-LX v1.0UC-8418-LX v1.1
  • 6. DA-681/682-LX v1.0Reference1. sed – An Introduction and tutorial http://www.grymoire.com/Unix/Sed.html2. Regular Expression http://www.grymoire.com/Unix/Regular.html#uh-2