⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linux tutorial-4.htm

📁 Linux Tutorial
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0066)http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux4.htm -->
<HTML><HEAD><TITLE>Linux Tutorial</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.2800.1226" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId></HEAD>
<BODY>
<TABLE width=600 border=0>
  <TBODY>
  <TR>
    <TD width=9></TD>
    <TD width=577>
      <P align=center><I><B><A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/index.htm">Home</A> 
      - <A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux1.htm">(1) 
      Shells</A> - <A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux2.htm">(2) 
      Files &amp; Directories</A> - <A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux3.htm">(3) 
      Processes</A> - <A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux4.htm">(4)Shell 
      scripting</A> - <A 
      href="http://www.cs.ncl.ac.uk/modules/2003-04/csc842/LinuxTut/linux5.htm">(5)Makefiles</A></B></I></P></TD></TR>
  <TR>
    <TD width=9></TD>
    <TD width=577><FONT face="Times New Roman" size=5>Linux Tutorial - User's 
      view (4)</FONT> 
      <P><FONT face="Times New Roman">Subject Summary</FONT></P>
      <OL>
        <LI><FONT face="Times New Roman">Input &amp; output</FONT> 
        <LI><FONT face="Times New Roman">Variables</FONT> 
        <LI><FONT face="Times New Roman">Maths</FONT> 
        <LI><FONT face="Times New Roman">Loops &amp; if statement</FONT> 
</LI></OL>
      <P><FONT face="Times New Roman" size=5><B><I>Scripts</I></B></FONT></P>
      <P>A term for macro or batch file, a script is a list of commands that can 
      be executed with or without user interaction. A script language is a 
      simple programming language with which you can write scripts.</P>
      <P>We can write scripts and with the aid of the shell, we can execute 
      them.</P>
      <P><FONT size=4>1. Hello World!</FONT></P>
      <P>All scripts should be contained in a file, with this file given 
      executable privileges (Linux Tutorial - User's view (1) for information on 
      how to do this. Any text editor will do. <B>Emacs</B> is a popular editor 
      and should be available on most systems (simply type <B>emacs</B> at the 
      command line to get the editor). Editors with a graphical interface are 
      easiest to use for beginners. Ask your system administrator where the 
      graphical text editor is located. If you think you will become a hardened 
      computer user then <B>vi</B> can be used.</P>
      <UL type=circle>
        <LI><B>#!/bin/sh&nbsp;</B> 
        <LI><B>echo "Hello"&nbsp;</B> 
        <LI><B>echo "World"&nbsp;</B> </LI></UL>
      <P>The above script prints out on the screen (via the echo command) "Hello 
      Word". Notice that the first line of the script is actually a special line 
      of code that indicates the shell that is required to parse this script to 
      execute it successfully.&nbsp;</P>
      <P><FONT size=4>2. Variables</FONT></P>
      <P>Variables (user defined in the script and environmental) may be used 
      within scripts. Consider the following script:</P>
      <UL type=circle>
        <LI><B>#!/bin/sh&nbsp;</B> 
        <LI><B>echo "Hi there"</B> 
        <LI><B>echo "Type in your real name Slim Shady?"</B> 
        <LI><B>read ENM</B> 
        <LI><B>echo "Hello $ENM"</B> </LI></UL>
      <P>Here we have defined a variable called <B>ENM</B> and used it to store 
      some user input. Notice how the <B>read</B> command is used to gain the 
      user interaction.</P>
      <P><FONT size=4>3. Maths</FONT></P>
      <P>Scripts may also carry out simple arithmetic for you. Consider the 
      following:</P>
      <UL type=circle>
        <LI><B>#!/bin/sh</B> 
        <LI><B>if [ "$2" ]</B> 
        <LI><B>then</B> 
        <LI><B>answer=`expr $1 + $2`</B> 
        <LI><B>echo "Answer = $answer"</B> 
        <LI><B>else</B> 
        <LI><B>echo "This script needs two arguments"</B> 
        <LI><B>fi</B> 
        <LI><B>echo "Finished"</B> </LI></UL>
      <P><FONT size=4>4. Loops</FONT></P>
      <P>In programming environments it is always useful to enable a programming 
      structure that allows for a repetition of a task without actually typing 
      this task out multiple times. Scripts provide this mechanism via looping 
      structures. The while loop is commonly used in scripts:</P>
      <UL type=circle>
        <LI><B>#!/bin/sh</B> 
        <LI><B>dayname=1</B> 
        <LI><B>while [ $dayname -le 7 ]</B> 
        <LI><B>do</B> 
        <LI><B>echo "Day number is $dayname"</B> 
        <LI><B>dayname=`expr $dayname + 1`</B> 
        <LI><B>done</B> 
        <LI><B>echo "Finished"<BR></B></LI></UL>
      <P>The dayname=1 creates a variable called <B>dayname</B> and places the 
      number <B>1</B> into it. The <B>while</B> command executes all the 
      commands between the <B>do</B> and the <B>done</B> repetitively until the 
      `` test'' condition is no longer true (i.e until <B>dayname</B> is no 
      longer less than 7. The <B>-le</B> stands for -less-than-or-equal-to.</P>
      <P><FONT size=4>5. If statement</FONT></P>
      <P>Sometimes it is required that a choice of execution be made in a script 
      depending on some influence of the environment (e.g., user choice). The if 
      statement satisfies this requirements in scripting:</P>
      <UL type=circle>
        <LI><B>#!/bin/sh&nbsp;</B> 
        <LI><B>if [ 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -