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

📄 writingscripts.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Writing Scripts</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Exercises"HREF="exercises.html"><LINKREL="PREVIOUS"TITLE="Exercises"HREF="exercises.html"><LINKREL="NEXT"TITLE="Revision History"HREF="revisionhistory.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="exercises.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Appendix M. Exercises</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="revisionhistory.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="WRITINGSCRIPTS"></A>M.2. Writing Scripts</H1><P><ANAME="WRITINGSCRIPTS1"></A></P><P>Write a script to carry out each of the following tasks.</P><DIVCLASS="VARIABLELIST"><P><B><ANAME="EXEASY1"></A>EASY</B></P><DL><DT><BCLASS="COMMAND">Home Directory Listing</B></DT><DD><P>Perform a recursive directory listing on the user's home	      directory and save the information to a file. Compress	      the file, have the script prompt the user to insert	      a floppy, then press <BCLASS="KEYCAP">ENTER</B>. Finally,	      save the file to the floppy.</P></DD><DT><BCLASS="COMMAND">Self-reproducing Script</B></DT><DD><P>Write a script that backs itself up, that is, copies	      itself to a file named <TTCLASS="FILENAME">backup.sh</TT>.</P><P>Hint: Use the <AHREF="external.html#CATREF">cat</A> command	      and the <AHREF="othertypesv.html#SCRNAMEPARAM">appropriate positional	      parameter</A>.</P></DD><DT><BCLASS="COMMAND">Converting  <AHREF="loops.html#FORLOOPREF1">for</A>	    loops to <AHREF="loops.html#WHILELOOPREF">while</A> and <AHREF="loops.html#UNTILLOOPREF">until</A> loops</B></DT><DD><P>Convert the <ICLASS="FIRSTTERM">for loops</I> in <AHREF="loops.html#EX22">Example 10-1</A> to <ICLASS="FIRSTTERM">while	      loops</I>. Hint: store the data in an <AHREF="arrays.html#ARRAYREF">array</A> and step through the array	      elements.</P><P>Having already done the <SPANCLASS="QUOTE">"heavy lifting"</SPAN>,	      now convert the loops in the example to <ICLASS="FIRSTTERM"> until	      loops</I>.</P></DD><DT><BCLASS="COMMAND">Changing the line spacing of a text file</B></DT><DD><P>Write a script that reads each line of a target file, then	      writes the line back to <TTCLASS="FILENAME">stdout</TT>, but with	      an extra blank line following. This has the effect of	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">double-spacing</I></SPAN> the file.</P><P>Include all necessary code to check whether the script	      gets the necessary command line argument (a filename),	      and whether the specified file exists.</P><P>When the script runs correctly, modify it to	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">triple-space</I></SPAN> the target file.</P><P>Finally, write a script to remove all blank lines from	      the target file, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">single-spacing</I></SPAN> it.</P></DD><DT><BCLASS="COMMAND">Backwards Listing</B></DT><DD><P>Write a script that echoes itself to	      <TTCLASS="FILENAME">stdout</TT>, but	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">backwards</I></SPAN>.</P></DD><DT><BCLASS="COMMAND">Automatically Decompressing Files</B></DT><DD><P>Given a list of filenames as input, this script	      queries each target file (parsing the output of the	      <AHREF="filearchiv.html#FILEREF">file</A> command) for	      the type of compression used on it. Then the script	      automatically invokes the appropriate decompression command	      (<BCLASS="COMMAND">gunzip</B>, <BCLASS="COMMAND">bunzip2</B>,	      <BCLASS="COMMAND">unzip</B>, <BCLASS="COMMAND">uncompress</B>,	      or whatever). If a target file is not compressed, the	      script emits a warning message, but takes no other action	      on that particular file.</P></DD><DT><BCLASS="COMMAND">Unique System ID</B></DT><DD><P>Generate a <SPANCLASS="QUOTE">"unique"</SPAN> 6-digit hexadecimal	      identifier for your computer. Do <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN>	      use the flawed <AHREF="system.html#HOSTIDREF">hostid</A>	      command. Hint: <BCLASS="COMMAND"><AHREF="filearchiv.html#MD5SUMREF">md5sum</A>	      <TTCLASS="FILENAME">/etc/passwd</TT></B>, then select	      the first 6 digits of output.</P></DD><DT><BCLASS="COMMAND">Backup</B></DT><DD><P>Archive as a <SPANCLASS="QUOTE">"tarball"</SPAN>	      (<TTCLASS="FILENAME">*.tar.gz</TT> file) all the files	      in your home directory tree	      (<TTCLASS="FILENAME">/home/your-name</TT>) that have	      been modified in the last 24 hours. Hint: use <AHREF="moreadv.html#FINDREF">find</A>.</P></DD><DT><BCLASS="COMMAND">Checking whether a process is still running</B></DT><DD><P>Given a <AHREF="special-chars.html#PROCESSIDREF">process ID</A>	      (<ICLASS="FIRSTTERM">PID</I>) as an argument, this script	      will check, at user-specified intervals, whether	      the given process is still running. You may use	      the <AHREF="system.html#PPSSREF">ps</A> and <AHREF="timedate.html#SLEEPREF">sleep</A> commands.</P></DD><DT><BCLASS="COMMAND">Primes</B></DT><DD><P>Print (to stdout) all prime numbers between 60000 and	      63000. The output should be nicely formatted in columns	      (hint: use <AHREF="internal.html#PRINTFREF">printf</A>).</P></DD><DT><BCLASS="COMMAND">Lottery Numbers</B></DT><DD><P>One type of lottery involves picking five	      different numbers, in the range of 1 - 50. Write a	      script that generates five pseudorandom numbers in this	      range, <SPANCLASS="emphasis"><ICLASS="EMPHASIS">with no duplicates</I></SPAN>. The	      script will give the option of echoing the numbers to	      <TTCLASS="FILENAME">stdout</TT> or saving them to a file,	      along with the date and time the particular number set	      was generated.</P></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="EXMEDIUM1"></A>INTERMEDIATE</B></P><DL><DT><BCLASS="COMMAND">Integer or String</B></DT><DD><P>Write a script <AHREF="functions.html#FUNCTIONREF">function</A>	      that determines if an argument passed to it is an integer	      or a string. The function will return TRUE (0) if	      passed an integer, and FALSE (1) if passed a string.</P><P>Hint: What does the following expression return	      when <TTCLASS="VARNAME">$1</TT> is <SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN>	      an integer?</P><P><TTCLASS="VARNAME">expr $1 + 0</TT></P></DD><DT><BCLASS="COMMAND">Managing Disk Space</B></DT><DD><P>List, one at a time, all files larger than 100K in	      the <TTCLASS="FILENAME">/home/username</TT>	      directory tree. Give the user the option to delete or	      compress the file, then proceed to show the next one. Write	      to a logfile the names of all deleted files and the	      deletion times.</P></DD><DT><BCLASS="COMMAND">Removing Inactive Accounts</B></DT><DD><P>Inactive accounts on a network waste disk space and may	      become a security risk. Write an administrative script	      (to be invoked by <ICLASS="FIRSTTERM">root</I> or the <AHREF="system.html#CRONREF">cron daemon</A>) that checks	      for and deletes user accounts that have not been accessed	      within the last 90 days.</P></DD><DT><BCLASS="COMMAND">Enforcing Disk Quotas</B></DT><DD><P>Write a script for a multi-user system that checks users'	      disk usage. If a user surpasses the preset limit	      (100 MB, for example) in her <TTCLASS="FILENAME">/home/username</TT> directory,	      then the script will automatically send her a warning	      e-mail.</P><P>The script will use the <AHREF="system.html#DUREF">du</A> and	      <AHREF="communications.html#COMMMAIL1">mail</A> commands. As an option,	      it will allow setting and enforcing quotas using the <AHREF="system.html#QUOTAREF">quota</A> and <AHREF="system.html#SETQUOTAREF">setquota</A> commands.</P></DD><DT><BCLASS="COMMAND">Logged in User Information</B></DT><DD><P>For all logged in users, show their real names and the time              and date of their last login.</P><P>Hint: use <AHREF="system.html#WHOREF">who</A>,	      <AHREF="system.html#LASTLOGREF">lastlog</A>,	      and parse <TTCLASS="FILENAME">/etc/passwd</TT>.</P></DD><DT><BCLASS="COMMAND">Safe Delete</B></DT><DD><P>Implement, as a script, a <SPANCLASS="QUOTE">"safe"</SPAN> delete	      command, <TTCLASS="FILENAME">sdel.sh</TT>. Filenames passed as	      command-line arguments to this script are not deleted,	      but instead <AHREF="filearchiv.html#GZIPREF">gzipped</A>	      if not already compressed (use <AHREF="filearchiv.html#FILEREF">file</A> to check), then moved	      to a <TTCLASS="FILENAME">~/TRASH</TT>	      directory. Upon invocation, the script checks the <TTCLASS="FILENAME">~/TRASH</TT> directory for files	      older than 48 hours and <AHREF="external.html#RMREF">permanently	      deletes</A> them. (An better alternative might be to	      have a second script handle this -- periodically invoked	      by the <AHREF="system.html#CRONREF">cron daemon</A>.)</P><P><SPANCLASS="emphasis"><ICLASS="EMPHASIS">Extra credit:</I></SPAN> Write the script	      so it can handle files and directories <AHREF="external.html#RMRECURS">recursively</A>. This would give it	      the capability of <SPANCLASS="QUOTE">"safely deleting"</SPAN> entire	      directory structures.</P></DD><DT><BCLASS="COMMAND">Making Change</B

⌨️ 快捷键说明

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