Bash 是我们经常与之打交道的 Shell 程序,本文针对其使用技巧进行了搜罗。相信在你看过这些内容之后,定会在 Bash 的世界里游刃有余。

  • 从历史中执行命令 有时候,我们需要在 Bash 中重复执行先前的命令。你当然可以使用上方向键来查看之前曾经运行过的命令。但这里有一种更好的方式:你可以按 Ctrl + r 组合键进入历史搜索模式,一旦找到需要重复执行的命令,按回车键即可。
  • 重复命令参数 先来看一个例子: mkdir /path/to/exampledir cd !$ 本例中,第一行命令将创建一个目录,而第二行的命令则转到刚创建的目录。这里,“!$”的作用就是重复前一个命令的参数。事实上,不仅是命令的参数可以重复,命令的选项同样可以。另外,Esc + . 快捷键可以切换这些命令参数或选项。
  • 用于编辑的快捷键
    • Ctrl + a:将光标定位到命令的开头
    • Ctrl + e:与上一个快捷键相反,将光标定位到命令的结尾
    • Ctrl + u:剪切光标之前的内容
    • Ctrl + k:与上一个快捷键相反,剪切光标之后的内容
    • Ctrl + y:粘贴以上两个快捷键所剪切的内容
    • Ctrl + t:交换光标之前两个字符的顺序
    • Ctrl + w:删除光标左边的参数(选项)或内容
    • Ctrl + l:清屏
  • 处理作业 首先,使用 Ctrl + z 快捷键可以让正在执行的命令挂起。如果要让该进程在后台执行,那么可以执行 bg 命令。而 fg 命令则可以让该进程重新回到前台来。使用 jobs 命令能够查看到哪些进程在后台执行。 你也可以在 fg 或 bg 命令中使用作业 id,如: fg %3 又如: bg %7
  • 使用置换
    • 命令置换 先看例子: du -h -a -c $(find . -name *.conf 2>&-) 注意 $() 中的部分,这将告诉 Bash 运行 find 命令,然后把返回的结果作为 du 的参数。
    • 进程置换 仍然先看例子: diff <(ps axo comm) <(ssh user@host ps axo comm) 该命令将比较本地系统和远程系统中正在运行的进程。请注意 <() 中的部分。
    • xargs 看例: find . -name *.conf -print0 | xargs -0 grep -l -Z mem_limit | xargs -0 -i cp {} {}.bak 该命令将备份当前目录中的所有 .conf 文件。
  • 使用管道 下面是一个简单的使用管道的例子: ps aux | grep init 这里,“|”操作符将 ps aux 的输出重定向给 grep init。 下面还有两个稍微复杂点的例子: ps aux | tee filename | grep init 及: ps aux | tee -a filename | grep init
  • 将标准输出保存为文件 你可以将命令的标准输出内容保存到一个文件中,举例如下: ps aux > filename 注意其中的“>”符号。 你也可以将这些输出内容追加到一个已存在的文件中: ps aux >> filename 你还可以分割一个较长的行: command1 | command2 | ... | commandN > tempfile1 cat tempfile1 | command1 | command2 | ... | commandN > tempfile2
  • 标准流:重定向与组合 重定向流的例子: ps aux 2>&1 | grep init 这里的数字代表:
    • 0:stdin
    • 1:stdout
    • 2:sterr
    上面的命令中,“grep init”不仅搜索“ps aux”的标准输出,而且搜索 sterr 输出。
    1. 建立层级目录:使用 mkdir 的 -p 选项,如 mkdir -p tmp/a/b/c。 2. 解包到指定的目录:使用 tar 的 -C 选项,如 tar xvf newarc.tar.gz -C tmp/a/b/c。 3. 联合命令:使用 ;、&&、|| 等控制运算符,如 cd tmp/a/b/c && tar xvf ~/archive.tar。 4. 小心使用变量:把变量放到 “” 中,如 ~ $ ls tmp/ a b ~ $ VAR="tmp/*" ~ $ echo $VAR tmp/a tmp/b ~ $ echo "$VAR" tmp/* ~ $ echo $VARa ~ $ echo "$VARa" ~ $ echo "${VAR}a" tmp/*a ~ $ echo ${VAR}a tmp/a ~ $ 5. 长命令的输入:使用 \ 分行折断,如 ~ $ cd tmp/a/b/c || \ > mkdir -p tmp/a/b/c && \ > tar xvf -C tmp/a/b/c ~/archive.tar 6. 分组命令:使用 ()、{} 来分组命令,如 ~ $ ( cd tmp/a/b/c/ || mkdir -p tmp/a/b/c && \ > VAR=$PWD; cd ~; tar xvf -C $VAR archive.tar ) \ > | mailx admin -S "Archive contents" 7. 使用 xargs:可以过滤输出,如 ~/tmp $ ls -l | xargs -rw-r--r-- 7 joe joe 12043 Jan 27 20:36 December_Report.pdf -rw-r--r-- 1 \ root root 238 Dec 03 08:19 README drwxr-xr-x 38 joe joe 354082 Nov 02 \ 16:07 a -rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar -rwxr-xr-x 1 \ joe joe 3239 Sep 30 12:40 mkdirhier.sh ~/tmp $ 8. 使用 grep 的 -c 选项可以计算输出的行数,它比使用管道的 wc -l 更快,如 ~ $ time grep and tmp/a/longfile.txt | wc -l 2811 real 0m0.097s user 0m0.006s sys 0m0.032s ~ $ time grep -c and tmp/a/longfile.txt 2811 real 0m0.013s user 0m0.006s sys 0m0.005s ~ $ 9. 匹配输出的字段:使用 awk,如 ~/tmp $ ls -l | awk '$6 == "Dec"' -rw-r--r-- 3 joe joe 5096 Dec 14 14:26 archive.tar -rw-r--r-- 1 root root 238 Dec 03 08:19 README ~/tmp $ 10. 停用 cat 的管道输出:可用 grep 代替,如 ~ $ time cat tmp/a/longfile.txt | grep and 2811 real 0m0.015s user 0m0.003s sys 0m0.013s ~ $ time grep and tmp/a/longfile.txt 2811 real 0m0.010s user 0m0.006s sys 0m0.004s ~ $
    1, 主板信息   .查看主板的序列号   --------------------------------------------------   #使用命令   dmidecode | grep -i 'serial number'   #查看板卡信息   cat /proc/pci   --------------------------------------------------   2, cpu信息   --------------------------------------------------   #通过/proc文件系统   1) cat /proc/cpuinfo   #通过查看开机信息   2) dmesg | grep -i 'cpu'   #   3)dmidecode -t processor 一、CPU大小 [root@idc ~]# cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep "physical id" model name: Intel(R) Xeon(TM) CPU 2.80GHz model name: Intel(R) Xeon(TM) CPU 2.80GHz model name: Intel(R) Xeon(TM) CPU 2.80GHz model name: Intel(R) Xeon(TM) CPU 2.80GHz physical id : 0 physical id : 0 physical id : 3 physical id : 3 [root@idc ~]# 说明:Linux下可以在/proc/cpuinfo中看到每个cpu的详细信息。但是对于双核的cpu,在cpuinfo中会看到两个cpu。常常会让人误以为是两个单核的cpu。 其实应该通过Physical Processor ID来区分单核和双核。而Physical Processor ID可以从cpuinfo或者dmesg中找到. flags 如果有 ht 说明支持超线程技术 判断物理CPU的个数可以查看physical id 的值,相同则为同一个物理CPU 可以看到上面,这台机器有两个双核的CPU,ID分别是0和3,大小是2.8G。 二、内存大小 [root@xbidc ~]# cat /proc/meminfo |grep MemTotal MemTotal: 1034612 kB [root@xbidc ~]# 三、硬盘大小 [root@xbidc ~]# fdisk -l |grep Disk Disk /dev/sda: 300.0 GB, 300000000000 bytes   --------------------------------------------------   3, 硬盘信息   --------------------------------------------------   #查看分区情况   fdisk -l   #查看大小情况   df -h   #查看使用情况   du -h   #   hdparm -I /dev/sda   #   dmesg | grep sda   --------------------------------------------------   4, 内存信息   --------------------------------------------------   1) cat /proc/meminfo   2) dmesg | grep mem   3) free -m   4) vmstat   5) dmidecode | grep -i mem   --------------------------------------------------   5, 网卡信息   --------------------------------------------------   1) dmesg | grep -i 'eth'   2) cat /etc/sysconfig/hwconf | grep -i eth   3) lspci | grep -i 'eth'   --------------------------------------------------   6, 鼠标键盘和USB信息   查看键盘和鼠标:cat /proc/bus/input/devices   查看USB设备:cat /proc/bus/usb/devices   查看各设备的中断请求(IRQ):cat /proc/interrupts   7, 显卡信息   --------------------------------------------------   1)lspci |grep -i 'VGA'   2)dmesg | grep -i 'VGA'   --------------------------------------------------   8, 声卡信息   --------------------------------------------------   1)lspci |grep -i 'VGA'   2)dmesg | grep -i 'VGA'   --------------------------------------------------   7, 其他命令   .用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart)   .dmesg (查看所有启动时检测到的硬件信息)   .lspci (显示外设信息, 如usb,网卡等信息)   .cat /etc/sysconfig/hwconf   .mpstat   8, 需要手动安装的工具   lshw,hwinfo,hal-device-manager   9, Solaris如何检测硬件参数   俺从别处发现了些有意思的东西:   Solaris的硬件相关命令   发表:2004-3-8 11:20:36 出处:你的博客网(yourblog.org)   --------------------------------------------------------------------------------   1).查看当前处理器的类型和速度(主频)   # psrinfo –v   Status of processor 1 as of: 11/24/01 10:34:41   Processor has been on-line since 11/24/01 10:18:20.   The sparcv9 processor operates at 432 MHz,   and has a sparcv9 floating point processor.   Status of processor 3 as of: 11/24/01 10:34:41   Processor has been on-line since 11/24/01 10:18:22.   The sparcv9 processor operates at 432 MHz,   and has a sparcv9 floating point processor.   2).打印当前的OBP版本号   # prtconf –V   OBP 3.20.0 2000/10/24 10:47   # /usr/platform/sun4u/sbin/prtdiag –v | grep OBP   OBP 3.20.0 2000/10/24 10:47 POST 6.1.0 2000/10/24 10:49   ok. .version   Release 3.20 Version 0 created 2000/10/24 10:47   OBP 3.20.0 2000/10/24 10:47   POST 6.1.0 2000/10/24 10:49   OBDIAG 4.5.1 2000/10/24 10:48   3).查看硬盘物理信息(vendor, RPM, Capacity)   # iostat –E   sd0 Soft Errors: 0 Hard Errors: 0 Transport Errors: 0   Vendor: SEAGATE Product: ST34371W SUN4.2G Revision: 7462 Serial No:   JDX394220KW EBC   Size: 4.29GB ;   Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0   Illegal Request: 0 Predictive Failure Analysis: 0   sd2 Soft Errors: 0 Hard Errors: 0 Transport Errors: 0   Vendor: FUJITSU Product: MAJ3364M SUN36G Revision: 0804 Serial No: 01M18144   Size: 36.42GB ;   Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0   Illegal Request: 0 Predictive Failure Analysis: 0   sd3 Soft Errors: 0 Hard Errors: 0 Transport Errors: 0   Vendor: FUJITSU Product: MAJ3364M SUN36G Revision: 0804 Serial No: 01M16199   Size: 36.42GB ;   Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0   Illegal Request: 0 Predictive Failure Analysis: 0   sd21 Soft Errors: 0 Hard Errors: 0 Transport Errors: 0   Vendor: TOSHIBA Product: DVD-ROM SD-M1401 Revision: 1007 Serial No: 06/22/00   Size: 18446744073.71GB ;   Media Error: 0 Device Not Ready: 0 No Device: 0 Recoverable: 0   Illegal Request: 0 Predictive Failure Analysis: 0   4).查看磁盘的几何参数和分区信息   # prtvtoc /dev/rdsk/c0t0d0s0   * /dev/rdsk/c0t0d0s0 partition map   *   * Dimensions:   * 512 bytes/sector   * 135 sectors/track   * 16 tracks/cylinder   * 2160 sectors/cylinder   * 3882 cylinders   * 3880 accessible cylinders   *   * Flags:   * 1: unmountable   * 10: read-only   *   * Unallocated space:   * First Sector Last   * Sector Count Sector   * 8277120 103680 8380799   *   * First Sector Last   * Partition Tag Flags Sector Count Sector Mount Directory   0 2 00 0 2049840 2049839 /   1 3 01 2049840 615600 2665439   2 5 00 0 8380800 8380799   5 0 00 2665440 2458080 5123519 /opt   6 4 00 5123520 3073680 8197199 /usr   7 8 00 8197200 79920 8277119 /export/home   5).显示已经使用和未使用的i-node数目   # df –F ufs –o i   Filesystem iused ifree %iused Mounted on   /dev/dsk/c0t0d0s0 7859 479821 2% /   /dev/dsk/c0t0d0s6 37763 339517 10% /usr   /dev/dsk/c0t0d0s5 722 301102 0% /opt   /dev/dsk/c0t0d0s7 4 24380 0% /export/home   6).显示cpu使用率最高的进程   # ps –eo pid,pcpu,args | sort +1n   该命令输出当前系统进程的pid, CPU占用率及命令描述,并以pcpu来排序   7).显示内存占用率最高的进程   # ps –eo pid,vsz,args | sort +1n   该命令输出当前系统进程的pid,内存占用率及命令描述,并以vsz来排序   8).查看及启动系统的32位或64位内核模式   64位模式   # isalist –v   sparcv9+vis sparcv9 sparcv8plus+vis sparcv8plus sparcv8 sparcv8-fsmuld sparcv7   sparc   # isainfo –v   64-bit sparcv9 applications   32-bit sparc applications   # isainfo –b   64   启动64位内核模式   ok. boot kernel/sparcv9/unix   32位模式   # isalist –v   sparcv8plus+vis sparcv8plus sparcv8 sparcv8-fsmuld sparcv7 sparc   # isainfo –v   32-bit sparc applications   # isainfo –b   32   启动32位模式   ok. boot kernel/unix   9).查看当前的OpenWindows版本   # showrev –w   OpenWindows version:   X11 Version 6.4.1 5 November 2001   10).查看当前CDE的版本   # /usr/ccs/bin/what /usr/dt/bin/dtmail   /usr/dt/bin/dtmail:   CDE Version 1.4.6_06   CDEVersion1.4.6_06   11).测定当前的显示器刷新频率   /usr/sbin/ffbconfig –rev \?   Valid values for -res option are:   1024x768x60 [1]   1024x768x70 [1]   1024x768x75 [1] [2]   1024x768x77   1024x800x84   1152x900x66   1152x900x76   1280x800x76 [1] [2]   1280x1024x60 [1] [2]   1280x1024x67   1280x1024x76   1280x1024x85 [1] [2]   960x680x112s   960x680x108s   640x480x60 [1] [2]   640x480x60i [1]   768x575x50i [1]   1440x900x76 [1] [2]   1600x1000x66 [1] [2]   1600x1000x76 [1] [2]   1600x1280x76 [1] [2]   1920x1080x72 [1] [2]   1920x1080x76 [1] [2]   1920x1200x70 [1] [2]   1920x1200x75 [1] [2]   svga [1]   1152   1280   stereo   vga [1] [2]   ntsc [1]   pal [1]   none   Notes:   [1] monitor does not support this resolution.   [2] this version of FFB (FFB1) does not support this resolution   12).查看系统配置   # /usr/platform/sun4u/sbin/prtdiag –v   System Configuration: Sun Microsystems sun4u Sun Enterprise 450 (2 X   UltraSPAR   C-II 432MHz)   System clock frequency: 86 MHz   Memory size: 1024 Megabytes   ========================= CPUs =========================   Run Ecache CPU CPU   Brd CPU Module MHz MB Impl. Mask   --More--   --- --- ------- ----- ------ ------ ----   SYS 1 1 432 4.0 US-II 10.0   SYS 3 3 432 4.0 US-II 10.0   ========================= Memory =========================   Interlv. Socket Size   Bank Group Name (MB) Status   ---- ----- ------ ---- ------   0 none 1901 256 OK   0 none 1902 256 OK   0 none 1903 256 OK   0 none 1904 256 OK   0 none 1701 256 OK   0 none 1702 256 OK   ========================= IO Cards =========================   No failures found in System   ===========================   ========================= Environmental Status =========================   System Temperatures (Celsius):   ------------------------------   AMBIENT 20   CPU 1 40   CPU 3 40   =================================   Front Status Panel:   -------------------   Keyswitch position is in On mode.   System LED Status: POWER GENERAL ERROR ACTIVITY   [ ON] [OFF] [ ON]   DISK ERROR THERMAL ERROR POWER SUPPLY ERROR   [OFF] [OFF] [OFF]   Disk LED Status: OK = GREEN ERROR = YELLOW   DISK 2: [OK] DISK 3: [OK]   DISK 0: [OK] DISK 1: [EMPTY]   =================================   Fans:   -----   Fan Bank Speed Status   -------- ----- ------   CPU 49 OK   PWR 31 OK   Power Supplies:   ---------------   Supply Rating Temp Status   ------ ------ ---- ------   0 550 W 33 OK   1 550 W 33 OK   ========================= HW Revisions =========================   ASIC Revisions:   ---------------   STP2223BGA: Rev 4   STP2223BGA: Rev 4   STP2223BGA: Rev 4   STP2003QFP: Rev 1   STP2205BGA: Rev 1   System PROM revisions:   ----------------------   OBP 3.20.0 2000/10/24 10:47 POST 6.1.0 2000/10/24 10:49   # sysdef   *   * Hostid   *   80fee99b   *   * sun4u Configuration   *   *   * Devices   *   packages (driver not attached)   terminal-emulator (driver not attached)   deblocker (driver not attached)   obp-tftp (driver not attached)   disk-label (driver not attached)   SUNW,builtin-drivers (driver not attached)   sun-keyboard (driver not attached)   ufs-file-system (driver not attached)   chosen (driver not attached)   openprom (driver not attached)   client-services (driver not attached)   options, instance #0   aliases (driver not attached)   memory (driver not attached)   virtual-memory (driver not attached)   associations (driver not attached)   slot2disk (driver not attached)   slot2led (driver not attached)   slot2dev (driver not attached)   pci, instance #0   ebus, instance #0   auxio (driver not attached)   ……   # prtconf –D   System Configuration: Sun Microsystems sun4u   Memory size: 1024 Megabytes   System Peripherals (Software Nodes):   SUNW,Ultra-4   packages   terminal-emulator   deblocker   obp-tftp   disk-label   SUNW,builtin-drivers   sun-keyboard   ufs-file-system   chosen   openprom   client-services   options, instance #0 (driver name: options)   aliases   memory   virtual-memory   associations   slot2disk   slot2led   slot2dev   pci, instance #0 (driver name: pcipsy)   ebus, instance #0 (driver name: ebus)   auxio   power (driver name: power)   SUNW,pll   sc   se, instance #0 (driver name: se)   su, instance #0 (driver name: su)   …..   13).查看当前系统中已经应用的补丁   # showrev –p   Patch: 109618-01 Obsoletes: Requires: Incompatibles: Packages: SUNWeuxwe,   SUN   Weuezt, SUNWeudlg, SUNWeudda   Patch: 109889-01 Obsoletes: 109353-04 Requires: Incompatibles: Packages:   SUNWk   vmx, SUNWkvm, SUNWmdb, SUNWhea, SUNWpstl, SUNWpstlx   Patch: 110369-05 Obsoletes: 110709-02 Requires: Incompatibles: Packages:   SUNWk   vmx, SUNWcarx, SUNWcsr   Patch: 110370-03 Obsoletes: 111643-01 Requires: Incompatibles: Packages:   SUNWk   vmx, SUNWkvm, SUNWmdb, SUNWhea, SUNWpstl, SUNWpstlx   Patch: 110373-04 Obsoletes: 111508-01 Requires: Incompatibles: Packages:   SUNWk   vmx, SUNWcarx, SUNWcsr   Patch: 110374-08 Obsoletes: 110136-02, 110516-02 Requires: Incompatibles:   Pack   ages: SUNWkvmx, SUNWcarx, SUNWcar, SUNWcsr, SUNWmdbx   …..   14).显示当前的运行级别   # who –rH   NAME LINE TIME IDLE PID COMMENTS   . run-level 3 Nov 24 10:18 3 0 S   15).查找一个文件所从属的包   # pkgchk –lp /usr/lib/sendmail   Pathname: /usr/lib/sendmail   Type: regular file   Expected mode: 4555   Expected owner: root   Expected group: bin   Expected file size (bytes): 761368   Expected sum(1) of contents: 41707   Expected last modification: Sep 24 03:13:13 2001   Referenced by the following packages:   SUNWsndmu   Current status: installed   16).查看当前的bind版本信息   # nslookup –class=chaos –q=txt version.bind   Server: ns.lnpta.net.cn   Address: 202.96.64.68

    相关文章:

    • 2022-12-23
    • 2021-10-25
    • 2021-08-05
    • 2021-11-27
    • 2022-12-23
    • 2021-07-11
    • 2022-01-29
    猜你喜欢
    • 2022-12-23
    • 2021-07-03
    • 2021-05-29
    • 2022-01-21
    • 2021-09-05
    • 2022-12-23
    • 2022-12-23
    相关资源
    相似解决方案