LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: KornLee

【shell技巧交流区】:[把你的shell技巧贴出来,让我们分享]

[复制链接]
 楼主| 发表于 2003-7-10 16:38:07 | 显示全部楼层

技巧:妙用watch命令实时观察内存变化

在linux中,有一个很有趣的命令--watch,他的作用很有趣!
他作用是以全屏幕方式重复地执行指定的命令,用户可以通过他了解命令的运行情况.
如,我们要观察内存动态的变化,那么就可以:
watch free
这样就可以动态的观察内存中各个指标在指定时间内的变化啦~~,
如要观察虚拟内存的变化,也可以通过打开另外一个终端,而不耽误当前终端的操作!:
xterm -e watch -n 1 vmstat &
这样就会弹出一个xterm,显示有关虚拟内存的情况.
详细解释,请:
watch --help
man watch
 楼主| 发表于 2003-7-11 15:16:43 | 显示全部楼层

技巧: 用 tr 过滤文件[转贴]

了解文本实用程序

Jacek Artymiak(jacek@artymiak.com)
自由作家和顾问
2003 年 7 月

没有人曾说过 sed 很容易 - 它确实不容易!但通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能。Jacek Artymiak 向您展示如何去做。

您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符。您也可以用它来除去重复字符。这就是所有 tr 所能够做的。


那么,为什么要使用 tr,而不使用 sed 呢?当然是为了使事情简单。例如,如果我们希望用字母"z"来替换出现的所有字母"a",则可以用 tr a z,这条命令毫无疑问比 sed -e s/a/z/g 简单,尤其在把它用到脚本中时,其中的引号转义很让人头痛。另外,在使用 tr 时,可以避免写那些让人讨厌的正则表达式。


使用 tr 很简单:使用前面一段中所给出的符号表示法,用一个字符去替换出现的所有另一个字符。当需要替换多个字符时,使用类似于这样的表示法:tr abc xyz,它表示用字母"x"去替换出现的所有字母"a",用字母"y"去替换所有字母"b",用字母"z"去替换所有字母"c"。这两组中所列出的字符的数目不必相等。


您也可以指定字符的范围。例如,tr a-z A-Z 将用对应的大写字母来替换所有的小写字母(例如,它将"no smoking"转换成"NO SMOKING")。当您在 vi 编辑器中想强调正在编辑的文本的某一部分时,使用这一特殊技巧非常方便。只要按一下 Escape 键,然后按 : 键,再输入 2,4!tr 'a-z' 'A-Z',最后按一下 Return 键。现在,从第 2 行到第 4 行的字母就都转换成了大写字母。


关于 tr 的其它内容
GNU 手册上提到,tr 在执行您所选择的操作时,通过将标准输入复制到标准输出,从而实现"转换、压缩和/或删除字符"。在这篇技巧文章中,您将了解到这些选项;当然也可以通过了解 tr 的手册页或信息页,学习到更多关于 tr 的内容。

打开一个新的终端窗口,输入 man tr 或 info tr - 或者打开一个新的浏览器窗口,并链接到 gnu.org 上的 tr 手册页(关于这个链接,请参阅参考资料)。



另外,当有人给您发送了一个在 Mac OS 或 DOS/Windows 机器上创建的文本文件时,您会发现 tr 非常有用。如果没有将文件保存为使用 UNIX 换行符来表示行结束这种格式,则需要将这样的文件转换成本机 UNIX 格式,否则一些命令实用程序不会正确地处理这些文件。Mac OS 的行尾以回车字符结束,许多文本处理工具将这样的文件作为一行来处理。为了纠正这个问题,可以用下列技巧:

Mac -> UNIX:tr '\r' '\n' < macfile > unixfile
UNIX -> Mac:tr '\n' '\r' < unixfile > macfile

Microsoft DOS/Windows 约定,文本的每行以回车字符并后跟换行符结束。为了纠正这个问题,可以使用下列命令:

DOS -> UNIX:tr -d '\r' < dosfile > unixfile
UNIX -> DOS:在这种情况下,需要用 awk,因为 tr 不能插入两个字符来替换一个字符。要使用的 awk 命令为 awk '{ print $0"\r" }' < unixfile > dosfile

另外,当您需要对文本文件做一些简单的整理工作(如用 tr -d '\t' 除去制表符,用 tr -s ' ' 除去多余的空格,或者用 tr -d '\n' 将分开的几行合成一行)时,会需要用 tr。同样,可以在 vi 内使用所有这些命令;只要记住:在 tr 命令前要加上您希望处理的行范围和感叹号(!),如 1,$!tr -d '\t'(美元符号表示最后一行)中所示。


有什么问题,或是有什么见解吗?我很欢迎您的来信 - 请将邮件发至 jacek@artymiak.com
 楼主| 发表于 2003-7-22 02:54:49 | 显示全部楼层

技巧:删除文件中空行的几种方法

1,cat filename|tr -s '\n'
2,sed '/^$/d' filename
3,awk '{if($0!="")print}' filename
4,用grep也可以,但是比较麻烦;)
发表于 2003-7-23 22:48:55 | 显示全部楼层

这是我刚写的XF86Config配置程序,调用xf86cfg,请大家看看有什么值得改进的?

还没系统地学过shell,水平比较菜,还请大家别见笑!如果哪里需要改进的请发短信或电子邮件:hamigua@8bb8.com,多谢!
原贴请见:http://www.linuxsir.cn/forum.php?mod=viewthread&tid=53441
#!/bin/sh

#  This script was heavily modified by ROBERT LANGE hamigua hamigua@8bb8.com for use with the
#  You may reach http://www.linuxsir.cn explore even more helps

#  We are setting $HOME too /root so this will work wih the installation also.
HOME=/root

# If we aren't root, bail:
if [ "$USER" = "root" ]; then
  TMP=/var/log/setup/tmp
else
  echo "Only root can configure X."
  exit
fi

if ps -aA | grep "X" > /dev/null ; then
dialog --title "EXIT X SERVER?" --yesno \
"You are running the X server now,but this program can't run with it. \
I shall kill your X progress,after exit X server,please run 'xf4config' program to \
reconfigure once more. Do you want to continue ?
" 8 66
   if [ ! $? = 0 ]; then
   exit
   fi
   if ps -aA | grep "gdm-binary" > /dev/null ; then
   killall gdm-binary
   fi
   if ps -aA | grep "kdm" > /dev/null ; then
   killall kdm
   fi
   if ps -aA | grep "xdm" > /dev/null ; then
   killall xdm
   fi
   if ps -aA | grep "gnome-session" > /dev/null ; then
   killall gnome-session
   fi
   if ps -aA | grep "kdeinit" > /dev/null ; then
   killall kdeinit
   fi
   if ps -aA | grep "wmaker" > /dev/null ; then
   killall wmaker
   fi
   if ps -aA | grep "X" > /dev/null ; then
   killall X
   fi
fi

# Now, this wouldn't make much sense either:
if [ ! -r /usr/X11R6/bin/XFree86 ]; then
  exit
fi


dialog --title "CONFIGURE X SERVER?" --yesno \
"If you like, XFree86 can attempt to probe for your video hardware and mouse, and \
write an initial configuration file to /etc/X11/XF86Config.    Would you like to do this now ?       \
You can reach http://www.linuxsir.cn explore even more help. \
Establish by hamigua <hamigua@8bb8.com>" 9 66
if [ ! $? = 0 ]; then
  exit
fi

if [ ! -d $TMP ]; then
mkdir -p $TMP
chmod 700 $TMP
fi

# OK, we'll warn the user if there's already an existing XF86Config:
CONFIG_EXISTS=false
for xf86config in /etc/X11/XF86Config-4 /etc/XF86Config-4 /usr/X11R6/lib/X11/XF86Config-4 $HOME/XF86Config-4 ; do
  if [ -r $xf86config ]; then
    CONFIG_EXISTS=$xf86config
  fi  
done
if [ ! "$CONFIG_EXISTS" = "false" ]; then
  dialog --title "FOUND EXISTING XF86Config in `dirname $CONFIG_EXISTS`" \
  --msgbox "A previous X Window System configuration file has been found. \
You can now reconfigure X, replacing the file with a new version (and \
keeping a backup of the old file), or you can abort leaving the existing \
config file in place.  Hit ENTER to rename the XF86Config file to \
XF86Config.backup and create a new one, or ESC to abort." 9 72
  if [ ! $? = 0 ]; then
    exit
  fi
fi

# Have the X server create a default config file:
/usr/X11R6/bin/XFree86 -configure
if [ ! $? = 0 ]; then
  # failure, bail.
  exit
fi

# Move any existing config file(s) aside:
for xf86config in /etc/X11/XF86Config-4 /etc/XF86Config-4 /usr/X11R6/lib/X11/XF86Config-4 $HOME/XF86Config-4 ; do
  if [ -r $xf86config ]; then
    mv $xf86config ${xf86config}.backup
  fi  
done

mv $HOME/XF86Config.new /etc/X11/XF86Config
# Okay now we edit the little bugger to make it more usable
xf86cfg -textmode

# I know this completely hoses the indentation of the XF86Config file, but
# really don't know a good way around that.  Shoulda used perl.  ;)

cat /etc/X11/XF86Config | while read LINE ; do
  echo "$LINE" >> /etc/X11/XF86Config-4
  if echo $LINE | grep Load | grep type1 1> /dev/null ; then
    # X -configure is pretty broken... it leaves out the type1 and freetype
    # modules.  We'll work around this.  BTW, it matters not if these appear
    # multiple times, but matters A LOT if they don't appear at all.
    # echo "Load  \"type1\"" >> /etc/X11/XF86Config-4
    echo "Load  \"xtt\"" >> /etc/X11/XF86Config-4
  fi
# lets add wheel mouse support won't hurt if no wheel
    if echo $LINE | grep Option | grep Device | grep /dev/mouse 1> /dev/null ; then
        echo "Option      \"ZAxisMapping\"  \"4 5\"" >> /etc/X11/XF86Config-4
      fi
#All right no we add a couple more font paths and we are there
if echo $LINE | grep "/usr/X11R6/lib/X11/fonts/100dpi/" 1> /dev/null ; then
echo "FontPath     \"/usr/X11R6/lib/X11/fonts/TTF/\"" >> /etc/X11/XF86Config-4
echo "FontPath     \"/usr/X11R6/lib/X11/fonts/local/\"" >> /etc/X11/XF86Config-4
echo "FontPath     \"/usr/share/fonts/local/\"" >> /etc/X11/XF86Config-4
fi
#now lets add some monitor stuff
if echo $LINE | grep Section |grep Monitor 1> /dev/null ; then
ddcprobe  |grep "horizontal =" |sed 's%Timing ranges: horizontal =%HorizSync      %' |sed 's%, vertical =%        VertRefresh    %' >> /etc/X11/XF86Config-4
fi     
      
  done
rm -f $HOME/XF86Config.new
rm -f /etc/X11/XF86Config
# make it so normal users can use this config file

echo "Section \"DRI\"" >> /etc/X11/XF86Config-4
echo "        Mode 0666" >> /etc/X11/XF86Config-4
echo "EndSection" >> /etc/X11/XF86Config-4


dialog --title "XFree86 CONFIGURED" \
  --msgbox "Your new XFree86 configuration file has been saved to /etc/X11/XF86Config-4. \
You may still need to add or adjust some values in the file to achieve the desired \
results. For complete information about \
making these adjustments, please refer to. \"man XF86Config\" I'll go on X server now." \
  11 66
gdm
 楼主| 发表于 2003-7-24 03:06:25 | 显示全部楼层
谢谢哈兄!不过应该帖到[精华区]也许更恰当;)
发表于 2003-7-25 23:04:01 | 显示全部楼层

回复: 技巧:像执行命令一样执行脚本

最初由 javalee 发表
用自动导入函数的方法,可以在命令行下像执行命令一样快捷,而且速度快,占用资源少.
1,建立自己的函数库
mkdir functionlib
然后将常用的脚本改成函数的语法,如:
function filename { command ; }
将filename拷贝到functionlib中,
2,修改环境文件,在/etc/profile中添加
export FPATH=$HOME/functionlib
3,重登录一下
这样的话,你就可以随时用像ls那样运行你自己的filename"命令"
而不需要用什么dot,sh,来运行你的函数/脚本啦~~
如果在脚本中运行,可以在脚本顶部用
#!/bin/sh
##
autoload filename        //来自动导入函数.
...
filename                //调用函数
...


看不懂   版主能举一个完整的简单的实例吗??
 楼主| 发表于 2003-7-26 00:58:14 | 显示全部楼层
很简单,你按照上述步骤就可以的呀!~O~,
以我的为例:
1,mkdir funlib
2,cat tree
function tree {
du|awk '{print $2, "== ("$1/2"kb)"}'|sort -f|sed -e "s,[^ /]*/\([^ /]*\) ==,\|--\1," -e"s,[^ /]*/,| ,g"; }
把这个tree文件cp到funlib目录中,
3,把:
export FPATH=$HOME/funlib
加到/etc/profile文件中
4,运行:
. /etc/profile
就可以啦~~
发表于 2003-7-26 10:07:47 | 显示全部楼层
有一点不明白 怎么要先写一个再复制到funlib目录中

直接在它那里写行吗?
 楼主| 发表于 2003-7-26 11:40:13 | 显示全部楼层
行!
 楼主| 发表于 2003-7-27 16:13:20 | 显示全部楼层

技巧:如何判断输入的是字符还是数字的三个方法

1,用输入的字符串和任意一个数字进行运算,可以判断!
  1. #!/bin/ksh
  2. #
  3. var=$(echo "$1*1"|bc)
  4. if [[ $var != 0 ]]
  5. then
  6. echo "$1 is a number"
  7. else
  8. echo "$1 is a charter"
  9. fi
复制代码
2,用流编辑器sed!
  1. if [ -n "`echo $1|sed -n '/^[0-9][0-9]*$/p'`" ]
  2. then
  3. echo "$1 is number!"
  4. else
  5. echo "$1 is not number!"
  6. fi
复制代码
3,用awk来判断!
  1. echo $1|awk '{if($0~/[^0-9]/) {print "'$1' is not number"} else{print "'$1' is number"}}'
复制代码
来自:chinaunix.net
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表