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

📄 system.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
>ubstitute <BCLASS="COMMAND">u</B>ser.	      <BCLASS="COMMAND">su rjones</B> starts a shell as user	      <SPANCLASS="emphasis"><ICLASS="EMPHASIS">rjones</I></SPAN>. A naked <BCLASS="COMMAND">su</B>	      defaults to <ICLASS="FIRSTTERM">root</I>.  See <AHREF="contributed-scripts.html#FIFO">Example A-15</A>.</P></DD><DT><ANAME="SUDOREF"></A><BCLASS="COMMAND">sudo</B></DT><DD><P>Runs a command as <ICLASS="FIRSTTERM">root</I> (or	      another user). This may be used in a script, thus permitting	      a <ICLASS="FIRSTTERM">regular user</I> to run the script.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Some commands.   4&nbsp;sudo cp /root/secretfile /home/bozo/secret   5&nbsp;# Some more commands.</PRE></TD></TR></TABLE></P><P>The file <TTCLASS="FILENAME">/etc/sudoers</TT> holds	      the names of users permitted to invoke	      <BCLASS="COMMAND">sudo</B>.</P></DD><DT><ANAME="PASSWDREF"></A><BCLASS="COMMAND">passwd</B></DT><DD><P>Sets, changes, or manages a user's password.</P><P>The <BCLASS="COMMAND">passwd</B> command can be used in	      a script, but probably <SPANCLASS="emphasis"><ICLASS="EMPHASIS">should not</I></SPAN> be.</P><DIVCLASS="EXAMPLE"><HR><ANAME="SETNEWPW"></A><P><B>Example 16-1. Setting a new password</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;#  setnew-password.sh: For demonstration purposes only.   3&nbsp;#                      Not a good idea to actually run this script.   4&nbsp;#  This script must be run as root.   5&nbsp;   6&nbsp;ROOT_UID=0         # Root has $UID 0.   7&nbsp;E_WRONG_USER=65    # Not root?   8&nbsp;   9&nbsp;E_NOSUCHUSER=70  10&nbsp;SUCCESS=0  11&nbsp;  12&nbsp;  13&nbsp;if [ "$UID" -ne "$ROOT_UID" ]  14&nbsp;then  15&nbsp;  echo; echo "Only root can run this script."; echo  16&nbsp;  exit $E_WRONG_USER  17&nbsp;else  18&nbsp;  echo  19&nbsp;  echo "You should know better than to run this script, root."  20&nbsp;  echo "Even root users get the blues... "  21&nbsp;  echo  22&nbsp;fi    23&nbsp;  24&nbsp;  25&nbsp;username=bozo  26&nbsp;NEWPASSWORD=security_violation  27&nbsp;  28&nbsp;# Check if bozo lives here.  29&nbsp;grep -q "$username" /etc/passwd  30&nbsp;if [ $? -ne $SUCCESS ]  31&nbsp;then  32&nbsp;  echo "User $username does not exist."  33&nbsp;  echo "No password changed."  34&nbsp;  exit $E_NOSUCHUSER  35&nbsp;fi    36&nbsp;  37&nbsp;echo "$NEWPASSWORD" | passwd --stdin "$username"  38&nbsp;#  The '--stdin' option to 'passwd' permits  39&nbsp;#+ getting a new password from stdin (or a pipe).  40&nbsp;  41&nbsp;echo; echo "User $username's password changed!"  42&nbsp;  43&nbsp;# Using the 'passwd' command in a script is dangerous.  44&nbsp;  45&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>The <BCLASS="COMMAND">passwd</B> command's <TTCLASS="OPTION">-l</TT>,	      <TTCLASS="OPTION">-u</TT>, and <TTCLASS="OPTION">-d</TT> options permit	      locking, unlocking, and deleting a user's password. Only	      <ICLASS="FIRSTTERM">root</I> may use these options.</P></DD><DT><ANAME="ACREF"></A><BCLASS="COMMAND">ac</B></DT><DD><P>Show users' logged in time, as read from	      <TTCLASS="FILENAME">/var/log/wtmp</TT>. This is one of the GNU	      accounting utilities.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>ac</B></TT> <TTCLASS="COMPUTEROUTPUT">        total       68.08</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="LASTREF"></A><BCLASS="COMMAND">last</B></DT><DD><P>List <SPANCLASS="emphasis"><ICLASS="EMPHASIS">last</I></SPAN> logged in users, as read from	      <TTCLASS="FILENAME">/var/log/wtmp</TT>. This command can also	      show remote logins.</P><P>For example, to show the last few times the system	      rebooted:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>last reboot</B></TT> <TTCLASS="COMPUTEROUTPUT">reboot   system boot  2.6.9-1.667      Fri Feb  4 18:18          (00:02)     reboot   system boot  2.6.9-1.667      Fri Feb  4 15:20          (01:27)     reboot   system boot  2.6.9-1.667      Fri Feb  4 12:56          (00:49)     reboot   system boot  2.6.9-1.667      Thu Feb  3 21:08          (02:17)     . . . wtmp begins Tue Feb  1 12:50:09 2005</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="NEWGRPREF"></A><BCLASS="COMMAND">newgrp</B></DT><DD><P>Change user's <ICLASS="FIRSTTERM">group ID</I> without	      logging out. This permits access to the new group's	      files. Since users may be members of multiple groups	      simultaneously, this command finds only limited use.</P><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Kurt Glaesemann points out that the	      <ICLASS="FIRSTTERM">newgrp</I> command could prove helpful	      in setting the default group permissions for files a user	      writes. However, the <AHREF="system.html#CHGRPREF">chgrp</A>	      command might be more convenient for this purpose.</P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="TERMINALSSYS1"></A>Terminals</B></P><DL><DT><ANAME="TTYREF"></A><BCLASS="COMMAND">tty</B></DT><DD><P>Echoes the name of the current user's terminal.	      Note that each separate <ICLASS="FIRSTTERM">xterm</I>	      window counts as a different terminal.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>tty</B></TT> <TTCLASS="COMPUTEROUTPUT">/dev/pts/1</TT></PRE></TD></TR></TABLE></DD><DT><ANAME="STTYREF"></A><BCLASS="COMMAND">stty</B></DT><DD><P>Shows and/or changes terminal settings. This complex	      command, used in a script, can control terminal behavior	      and the way output displays. See the info page, and study	      it carefully.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ERASE"></A><P><B>Example 16-2. Setting an <ICLASS="FIRSTTERM">erase</I> character</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# erase.sh: Using "stty" to set an erase character when reading input.   3&nbsp;   4&nbsp;echo -n "What is your name? "   5&nbsp;read name                      #  Try to backspace   6&nbsp;                               #+ to erase characters of input.   7&nbsp;                               #  Problems?   8&nbsp;echo "Your name is $name."   9&nbsp;  10&nbsp;stty erase '#'                 #  Set "hashmark" (#) as erase character.  11&nbsp;echo -n "What is your name? "  12&nbsp;read name                      #  Use # to erase last character typed.  13&nbsp;echo "Your name is $name."  14&nbsp;  15&nbsp;exit 0  16&nbsp;  17&nbsp;# Even after the script exits, the new key value remains set.  18&nbsp;# Exercise: How would you reset the erase character to the default value?</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="SECRETPW"></A><P><B>Example 16-3. <ICLASS="FIRSTTERM">secret password</I>:	      Turning off terminal echoing </B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# secret-pw.sh: secret password   3&nbsp;   4&nbsp;echo   5&nbsp;echo -n "Enter password "   6&nbsp;read passwd   7&nbsp;echo "password is $passwd"   8&nbsp;echo -n "If someone had been looking over your shoulder, "   9&nbsp;echo "your password would have been compromised."  10&nbsp;  11&nbsp;echo &#38;&#38; echo  # Two line-feeds in an "and list."  12&nbsp;  13&nbsp;  14&nbsp;stty -echo    # Turns off screen echo.  15&nbsp;  16&nbsp;echo -n "Enter password again "  17&nbsp;read passwd  18&nbsp;echo  19&nbsp;echo "password is $passwd"  20&nbsp;echo  21&nbsp;  22&nbsp;stty echo     # Restores screen echo.  23&nbsp;  24&nbsp;exit 0  25&nbsp;  26&nbsp;# Do an 'info stty' for more on this useful-but-tricky command.</PRE></TD></TR></TABLE><HR></DIV><P>A creative use of <BCLASS="COMMAND">stty</B> is detecting a	      user keypress (without hitting	      <BCLASS="KEYCAP">ENTER</B>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="KEYPRESS"></A><P><B>Example 16-4. Keypress detection</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# keypress.sh: Detect a user keypress ("hot keys").   3&nbsp;   4&nbsp;echo   5&nbsp;   6&nbsp;old_tty_settings=$(stty -g)   # Save old settings (why?).   7&nbsp;stty -icanon   8&nbsp;Keypress=$(head -c1)          # or $(dd bs=1 count=1 2&#62; /dev/null)   9&nbsp;                              # on non-GNU systems  10&nbsp;  11&nbsp;echo  12&nbsp;echo "Key pressed was \""$Keypress"\"."  13&nbsp;echo  14&nbsp;  15&nbsp;stty "$old_tty_settings"      # Restore old settings.  16&nbsp;  17&nbsp;# Thanks, Stephane Chazelas.  18&nbsp;  19&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>Also see <AHREF="variables2.html#TIMEOUT">Example 9-3</A>.</P><P><ANAME="TERMINALSREF"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN13777"></A><P><B>terminals and modes</B></P><P>Normally, a terminal works in the	      <ICLASS="FIRSTTERM">canonical</I> mode.  When a user hits a	      key, the resulting character does not immediately go to	      the program actually running in this terminal.  A buffer	      local to the terminal stores keystrokes. When the user	      hits the <BCLASS="KEYCAP">ENTER</B> key, this sends all the	      stored keystrokes to the program running.  There is even	      a basic line editor inside the terminal.</P><P>	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>stty -a</B></TT> <TTCLASS="COMPUTEROUTPUT">speed 9600 baud; rows 36; columns 96; line = 0; intr = ^C; quit = ^\; erase = ^H; kill = ^U; eof = ^D; eol = &#60;undef&#62;; eol2 = &#60;undef&#62;; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; ... isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt</TT>                 </PRE></TD></TR></TABLE>	    </P><P>Using canonical mode, it is possible to redefine the              special keys for the local terminal line editor.	        <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat &#62; filexxx</B></TT> <TTCLASS="USERINPUT"><B>wha&#60;ctl-W&#62;I&#60;ctl-H&#62;foo bar&#60;ctl-U&#62;hello world&#60;ENTER&#62;</B></TT> <TTCLASS="USERINPUT"><B>&#60;ctl-D&#62;</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat filexxx</B></TT> <TTCLASS="COMPUTEROUTPUT">hello world</TT>		 <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>wc -c &#60; filexxx</B></TT> <TTCLASS="COMPUTEROUTPUT">12</TT>		                 </PRE></TD></TR></TABLE>              The process controlling the terminal receives only 12              characters (11 alphabetic ones, plus a newline), although              the user hit 26 keys.            </P><P>In non-canonical (<SPANCLASS="QUOTE">"raw"</SPAN>) mode, every              key hit (including special editing keys such as              <BCLASS="KEYCAP">ctl-H</B>) sends a character immediately to              the controlling process.</P><P>The Bash prompt disables both <TTCLASS="OPTION">icanon</TT>              and <TTCLASS="OPTION">echo</TT>, since it replaces the basic              terminal line editor with its own more elaborate one. For              example, when you hit <BCLASS="KEYCAP">ctl-A</B> at the Bash              prompt, there's no <BCLASS="KEYCAP">^A</B> echoed by the              terminal, but Bash gets a <BCLASS="KEYCAP">\1</B> character,              interprets it, and moves the cursor to the begining of              the line.</P><P><SPANCLASS="emphasis"><ICLASS="EMPHASIS">St閜hane Chazelas</I></SPAN></P></DIV></TD></TR></TABLE></DD><DT><ANAME="SETTERMREF"></A><BCLASS="COMMAND">setterm</B></DT><DD><P>Set certain terminal attributes. This command writes	      to its terminal's <TTCLASS="FILENAME">stdout</TT> a string that	      changes the behavior of that terminal.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>setterm -cursor off</B></TT> <TTCLASS="COMPUTEROUTPUT">bash$</TT> 	      </PRE></TD></TR></TABLE>	    </P><P>The <BCLASS="COMMAND">setterm</B> command can be used within a	      script to change the appearance of text written to	      <TTCLASS="FILENAME">stdout</TT>, although there are certainly	      <AHREF="colorizing.html#COLORIZINGREF">better tools</A> available	      for this purpose.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;setterm -bold on   2&nbsp;echo bold hello   3&nbsp;   4&nbsp;setterm -bold off   5&nbsp;echo normal hello</PRE></TD></TR></TABLE></P></DD><DT><ANAME="TSETREF"></A><BCLASS="COMMAND">tset</B></DT><DD><P>Show or initialize terminal settings. 	      This is a less capable version of	      <BCLASS="COMMAND">stty</B>.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR

⌨️ 快捷键说明

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