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

📄 ch22_22.htm

📁 the unix power tools
💻 HTM
📖 第 1 页 / 共 2 页
字号:
CLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist">&#13;<ACLASS="co"HREF="ch46_05.htm"TITLE="46.5 Stop Syntax Errors in String Tests ">x</A> <ACLASS="co"HREF="ch47_04.htm"TITLE="47.4 C Shell Variable Operators and Expressions ">!~</A> <ACLASS="co"HREF="ch45_07.htm"TITLE="45.7 The exec Command ">exec</A> <ACLASS="co"HREF="ch09_06.htm"TITLE="9.6 String Editing (Colon) Operators ">:q</A> &#13;</PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">&#13;#!/bin/csh -f# su - fix incorrect $HOME and USER with system 'su' commandforeach arg ($argv)   # find first non-option argument   if (&quot;x$arg&quot; !~ x-*) then      setenv HOME ~$arg      setenv USER $arg      exec /bin/su $argv:q   endifendecho &quot;$0 ERROR: can't find username.&quot;exit 1</PRE></TD></TR></TABLE></P><PCLASS="para">Another workaround for that is an alias with the name of the account I'm<EMCLASS="emphasis">su</EM>ing to:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch13_07.htm"TITLE="13.7 The () Subshell Operators ">(..)</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias randi '(setenv HOME ~randi; setenv USER randi; su randi)'</PRE></TD></TR></TABLE></P><PCLASS="para">There's another problem that can happen on any version of UNIX:the account you <EMCLASS="emphasis">su</EM> to<SPANCLASS="link">doesn't have permission (<ACLASS="linkend"HREF="ch22_02.htm"TITLE="Tutorial on File and Directory Permissions ">22.2</A>)</SPAN>to access the current directory where you ran the <EMCLASS="emphasis">su</EM> command.Then, you may get an error like <CODECLASS="literal">getwd: can't stat .</CODE> from the C shellon the account you <EMCLASS="emphasis">su</EM> to.Or you may get no error but the <EMCLASS="emphasis">su</EM> will fail.Thefix for both problems is to <EMCLASS="emphasis">cd</EM> to a world-access directory like<EMCLASS="emphasis">/</EM> or <EMCLASS="emphasis">/tmp</EM> before you run <EMCLASS="emphasis">su</EM>.An alias can make that easy:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"><ACLASS="co"HREF="ch10_06.htm"TITLE="10.6 Avoiding C Shell Alias Loops ">\su</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">alias su '(cd /; \su \!*)'</PRE></TD></TR></TABLE></P><PCLASS="para">You can also add the <CODECLASS="literal">cd /</CODE> command to this shell script if you want.</P><PCLASS="para">If the account you <EMCLASS="emphasis">su</EM> to runs the C shell (and you don't use the<EMCLASS="emphasis">-f</EM> option--see below), it will read the <EMCLASS="emphasis">.cshrc</EM> file.If that <EMCLASS="emphasis">.cshrc</EM> has hardcoded pathnames or commands that only theother account can run, the commands might fail.That can cause a variety of &quot;fun&quot; problems.Try replacing hardcoded pathnames like<CODECLASS="literal">/home/</CODE><CODECLASS="replaceable"><I>oldacct</I></CODE><CODECLASS="literal">/bin</CODE> with paths that use account-specificvariables like <CODECLASS="literal">$home/bin</CODE>, <CODECLASS="literal">~/bin</CODE>, and so on.</P><PCLASS="para">Plain <CODECLASS="literal">su</CODE> <CODECLASS="replaceable"><I>whoever</I></CODE> doesn't read aC shell user's <EMCLASS="emphasis">.login</EM> file or a Bourne shell user's <EMCLASS="emphasis">.profile</EM>.Using <CODECLASS="literal">su -</CODE> <CODECLASS="replaceable"><I>whoever</I></CODE> (see the section &quot;Other suFeatures&quot; at the end of this chapter) solvesthat,but you can't suspend an <EMCLASS="emphasis">su -</EM> shell (at least not on my systems).</P><PCLASS="para">Finally, because the <EMCLASS="emphasis">su</EM> command runs in a <SPANCLASS="link">subshell (<ACLASS="linkend"HREF="ch38_04.htm"TITLE="Subshells ">38.4</A>)</SPAN>,<SPANCLASS="link">environment variables (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>set in the account you <EMCLASS="emphasis">su</EM> from will be passed intothe subshell.That can be good or bad.For instance, your favorite<SPANCLASS="link">EDITOR (<ACLASS="linkend"HREF="ch06_03.htm"TITLE="Predefined Environment Variables ">6.3</A>)</SPAN>(<EMCLASS="emphasis">vi</EM>, Emacs, or whatever)can be passed to the account you <EMCLASS="emphasis">su</EM> to.But that account might also set a variable that you want to use.If you're wondering what's set after you <EMCLASS="emphasis">su</EM>, type <CODECLASS="literal">set</CODE> for alist of shell variables, and either<SPANCLASS="link"><EMCLASS="emphasis">env</EM> or <EMCLASS="emphasis">printenv</EM> (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>to see environment variables.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1920-SECT-1.4">22.22.4 Quick Changes to Other Accounts </A></H3><PCLASS="para">If your system is busy, it can take time to run through all the commandsin the other user's <EMCLASS="emphasis">.cshrc</EM> file.The <EMCLASS="emphasis">su</EM> command can pass arguments to the subshell it starts, though.If the other account uses C shell, the <EMCLASS="emphasis">-f</EM> option tells it not to readthe <EMCLASS="emphasis">.cshrc</EM> file (for example, <CODECLASS="literal">su -f </CODE><CODECLASS="replaceable"><I>whoever</I></CODE>).You won't get that account's <EMCLASS="emphasis">.cshrc</EM> setup, but you will start to work onit sooner.</P><PCLASS="para">If logging in on your system takes a long time and you want to switch toanother account permanently, you can<SPANCLASS="link"><EMCLASS="emphasis">exec</EM> (<ACLASS="linkend"HREF="ch45_07.htm"TITLE="The exec Command ">45.7</A>)</SPAN>the <EMCLASS="emphasis">su</EM> command:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">% <CODECLASS="userinput"><B>exec su </B></CODE><CODECLASS="replaceable"><I>whoever</I></CODE></PRE></BLOCKQUOTE></P><PCLASS="para">That makes a weird situation where the<SPANCLASS="link"><EMCLASS="emphasis">who</EM> (<ACLASS="linkend"HREF="ch51_04.htm"TITLE="Who's On? ">51.4</A>)</SPAN>command will show you loggedon as your original account, but you'll be running as <CODECLASS="replaceable"><I>whoever</I></CODE>(the <EMCLASS="emphasis">whoami</EM> or <EMCLASS="emphasis">id</EM> command will tell you that).Also, because the <EMCLASS="emphasis">su</EM> shell isn't a<SPANCLASS="link">login shell (<ACLASS="linkend"HREF="ch02_08.htm"TITLE="Identifying Login Shells ">2.8</A>)</SPAN>,the <EMCLASS="emphasis">logout</EM> commandwon't work; you'll need to type <CODECLASS="literal">exit</CODE> instead.So, <EMCLASS="emphasis">exec su</EM> is a little tricky-but it's fast.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="UPT-ART-1920-SECT-1.5">22.22.5 Other su Features </A></H3><PCLASS="para">The command <EMCLASS="emphasis">su -e</EM>, which may also be called <EMCLASS="emphasis">su -m</EM> or<EMCLASS="emphasis">su -p</EM>, switches to the other user's account but keepsthe environment you have now.That's handy when you're having trouble with the other user's environmentor want to keep your own.(Some <EMCLASS="emphasis">su</EM>s do <EMCLASS="emphasis">-m</EM> by default, more or less.Use<SPANCLASS="link"><EMCLASS="emphasis">env</EM> or <EMCLASS="emphasis">printenv</EM> (<ACLASS="linkend"HREF="ch06_01.htm#UPT-ART-1170"TITLE="What Environment Variables Are Good For ">6.1</A>)</SPAN>to see what you get.)</P><PCLASS="para">The command <EMCLASS="emphasis">su -</EM> simulates a full login to the other account.If the other account runs the Bourne shell, the <EMCLASS="emphasis">.profile</EM> will be read.For the C shell, both <EMCLASS="emphasis">.cshrc</EM> and <EMCLASS="emphasis">.login</EM> will be read.You can't <EMCLASS="emphasis">suspend</EM> a <EMCLASS="emphasis">su -</EM> subshell.When you log out though, you'll be back in your original account's shell.<ACLASS="indexterm"NAME="AUTOID-25151"></A></P></DIV><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">JP</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch22_21.htm"TITLE="22.21 How to Change File Ownership Without chown"><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 22.21 How to Change File Ownership Without chown"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="CHAPTER"HREF="ch23_01.htm"TITLE="23. Removing Files"><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 23. Removing Files"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">22.21 How to Change File Ownership Without chown</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">23. Removing Files</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed &amp; awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>

⌨️ 快捷键说明

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