- 论坛徽章:
- 2
|
本帖最后由 122285969 于 2011-05-08 16:17 编辑
学习Linux命令也有些时日了,今天把这份简单总结分享给大家
如果你是正在学习Linux命令的朋友,希望能给你带来些许帮助
有问题才会有方向,解决问题就是我们的方向
所以让我们带着下面6个问题出发……
当前用户直接能使用的命令都存在哪里呢?
当前用户直接能使用的命令一共有多少个?
我记不清命令拼写该怎么处理呢?
我怎么知道一个命令是干什么的?
我怎么知道一个命令如何使用呢?
当前目录的可执行文件怎么不能直接执行?
我们按照上面的顺序一一来解答:
当前用户直接能使用的命令都存在哪里呢?
我们先看看root用户:- [root@Centos ~]# whoami
- root
复制代码 PS:当前用户root- [root@Centos ~]# echo $PATH
- /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
复制代码 PS:Linux所有可以直接执行的命令都是存在 PATH 环境变量中以“:”分隔的路径下的- [root@Centos ~]# echo $PATH | sed 's/:/\n/g' | cat -n
- 1 /usr/kerberos/sbin
- 2 /usr/kerberos/bin
- 3 /usr/local/sbin
- 4 /usr/local/bin
- 5 /sbin
- 6 /bin
- 7 /usr/sbin
- 8 /usr/bin
- 9 /root/bin
复制代码 PS:默认root用户所有可以直接执行的命令,就存在这9个目录下
我们再看看一个普通用户的情况:- [ctu@Centos ~]$ whoami
- ctu
复制代码 PS:当前用户ctu- [ctu@Centos ~]$ echo $PATH
- /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ctu/bin
- [ctu@Centos ~]$ echo $PATH | awk -F ':' '{for (i=1; i<=NF; i++) {printf("%s\n",$i)}}' | cat -n
- 1 /usr/kerberos/bin
- 2 /usr/local/bin
- 3 /bin
- 4 /usr/bin
- 5 /home/ctu/bin
复制代码 PS:普通用户默认可以直接执行的命令,就存在这个5个目录下。我们可以看到,root跟普通用户的默认 PATH 环境变量的值并不相同,没在各自环境变量中指定路径的命令,就只能用绝对路径和相对路径来执行了。
当前用户直接能使用的命令一共有多少个?
我们先写一个测试脚本:- [root@Centos ~]# cat -n comm_sum_1.sh
- 1 #!/bin/sh
- 2
- 3 sum=0
- 4 count=0
- 5
- 6 echo -e "I'am $USER !\n"
- 7
- 8 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 9 do
- 10 count=`ls $comm_path 2>/dev/null | wc -l`
- 11 echo -e "$count\t: $comm_path"
- 12 sum=$[sum+count]
- 13 done
- 14
- 15 echo -e "\n$sum\t: comm_sum"
复制代码 我们以root用户运行一下试试看吧:- [root@Centos ~]# . comm_sum_1.sh
- I'am root !
- 14 : /usr/kerberos/sbin
- 20 : /usr/kerberos/bin
- 0 : /usr/local/sbin
- 0 : /usr/local/bin
- 264 : /sbin
- 111 : /bin
- 348 : /usr/sbin
- 1111 : /usr/bin
- 0 : /root/bin
- 1868 : comm_sum
复制代码 PS:提示我们root用户可以直接使用的命令一共有 1868 个? 真的是这样吗?
我们再写一个脚本,这次加入了可执行权限的判断,并把不能直接执行的命令记录在 no_allow_comm.txt 文件中- [root@Centos ~]# cat -n comm_sum_2.sh
- 1 #!/bin/sh
- 2
- 3 sum=0
- 4
- 5 echo -e "I'am $USER !\n"
- 6
- 7 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 8 do
- 9 count=0
- 10 for file in `ls $comm_path 2>/dev/null`
- 11 do
- 12 [ -x "$comm_path/$file" ] && count=$[count+1] || echo "$comm_path/$file" >> no_allow_comm.txt
- 13 done
- 14 echo -e "$count\t: $comm_path"
- 15 sum=$[sum+count]
- 16 done
- 17
- 18 echo -e "\n$sum\t: comm_sum"
- 19
复制代码 我们以脚本 comm_sum_2.sh 来检测试试看:- [root@Centos ~]# . comm_sum_2.sh
- I'am root !
- 14 : /usr/kerberos/sbin
- 20 : /usr/kerberos/bin
- 0 : /usr/local/sbin
- 0 : /usr/local/bin
- 264 : /sbin
- 111 : /bin
- 348 : /usr/sbin
- 1107 : /usr/bin
- 0 : /root/bin
- 1864 : comm_sum
复制代码 PS:啊?这个跟我们上次检测的结果有出入了- [root@Centos ~]# cat -n no_allow_comm.txt
- 1 /usr/bin/ecryptfs-dot-private
- 2 /usr/bin/huge_page_setup_helper.pyc
- 3 /usr/bin/huge_page_setup_helper.pyo
- 4 /usr/bin/pygtk-demo
- [root@Centos ~]# ecryptfs-dot-private
- -bash: /usr/bin/ecryptfs-dot-private: 权限不够
- [root@Centos ~]# ls -l /usr/bin/ecryptfs-dot-private
- -rw-r--r-- 1 root root 1154 2009-09-04 /usr/bin/ecryptfs-dot-private
复制代码 PS:我们再次检测的时候,提示一共有 1864 个了。还有4个命令,确实不可以直接执行。
用一个普通用户来试试看:- [ctu@Centos ~]$ . comm_sum_1.sh
- I'am ctu !
- 20 : /usr/kerberos/bin
- 0 : /usr/local/bin
- 111 : /bin
- 1111 : /usr/bin
- 0 : /home/ctu/bin
- 1242 : comm_sum
复制代码 PS:脚本1检测的结果是 1242 个- [ctu@Centos ~]$ . comm_sum_2.sh
- I'am ctu !
- 20 : /usr/kerberos/bin
- 0 : /usr/local/bin
- 111 : /bin
- 1105 : /usr/bin
- 0 : /home/ctu/bin
- 1236 : comm_sum
复制代码 PS:脚本2检测的结果是 1236 个,一样有出入- [ctu@Centos ~]$ cat -n no_allow_comm.txt
- 1 /usr/bin/amtu
- 2 /usr/bin/aulastlog
- 3 /usr/bin/ecryptfs-dot-private
- 4 /usr/bin/huge_page_setup_helper.pyc
- 5 /usr/bin/huge_page_setup_helper.pyo
- 6 /usr/bin/pygtk-demo
- [ctu@Centos ~]$ amtu
- -bash: /usr/bin/amtu: 权限不够
- [ctu@Centos ~]$ ls -l /usr/bin/amtu
- -rwxr-x--- 1 root root 33412 2008-05-25 /usr/bin/amtu
复制代码 PS:跟上面的结果是一样的,也有些命令虽然是在 PATH 环境变量中,仍然不可以直接执行的。
我记不清命令拼写该怎么处理呢?
我们以命令 ifconfig 为例说明:
我要使用这个命令,但是我不记得怎么拼写了,模糊地记得好像前面两个字符是 if 吧?
那就输入 if ,再按两下 TAB 键吧。- [root@Centos ~]# if
- if ifconfig ifenslave ifrename
- ifcfg ifdown ifnames ifup
复制代码 系统自动为我们找到了我们要使用的命令 ifconfig
但是 TAB 键的方法只适合我们记得前面几个字符的情况。
要是我们只记得第3,4个字符为 co 呢? 或者我们只记得命令中含有 fig 呢?这样的情况我们使用 TAB 键好像无能为力了。
显然 TAB 键在这些情况下并不适合,要必要寻找新的方法来替代了(这并不是 TAB 键功能的缺陷)。
我们写一个命令查找的shell脚本- [ctu@Centos ~]$ cat -n comm_find.sh
- 1 #!/bin/sh
- 2
- 3 read -p "Please enter a keyword : " comm_name
- 4
- 5 echo -e "I'am $USER !\n"
- 6
- 7 for comm_path in `echo $PATH | sed 's/:/\n/g'`
- 8 do
- 9 ls $comm_path 2>/dev/null | grep -iE "$comm_name" && echo -e "\n[ $comm_path ]\n"
- 10 done
复制代码 我们测试下只记得第3,4个字符是 co 的情况:- [root@Centos ~]# . comm_find.sh
- Please enter a keyword : ^..co
- I'am root !
- ifconfig
- iwconfig
- ldconfig
- [ /sbin ]
- dbconverter-2
- lvconvert
- pwconv
- racoon
- racoonctl
- vgconvert
- [ /usr/sbin ]
- chcon
- cscope
- cscope-indexer
- fgconsole
- opcontrol
- piconv
- recountdiff
- rfcomm
- secon
- sfconvert
- [ /usr/bin ]
复制代码 凭着我们的印象,相信你能找出来要使用的命令 ifconfig ,除非你从来就没使用过。
我们再看看只记得含有 fig 的情况:- [root@Centos ~]# . comm_find.sh
- Please enter a keyword : fig
- I'am root !
- krb5-config
- [ /usr/kerberos/bin ]
- chkconfig
- ifconfig
- iwconfig
- ldconfig
- plipconfig
- vconfig
- [ /sbin ]
- authconfig
- authconfig-gtk
- authconfig-tui
- hciconfig
- iconvconfig
- iconvconfig.i686
- system-config-authentication
- system-config-lvm
- system-config-network
- system-config-network-cmd
- system-config-network-gui
- system-config-network-tui
- system-config-packages
- sys-unconfig
- timeconfig
- [ /usr/sbin ]
- authconfig
- authconfig-gtk
- authconfig-tui
- curl-config
- dateconfig
- gpg-error-config
- libgcrypt-config
- libusb-config
- nspr-config
- nss-config
- pkg-config
- scim-config-agent
- scrollkeeper-config
- system-config-authentication
- system-config-date
- system-config-display
- system-config-kdump
- system-config-keyboard
- system-config-language
- system-config-lvm
- system-config-network
- system-config-network-cmd
- system-config-rootpassword
- system-config-securitylevel
- system-config-securitylevel-tui
- system-config-soundcard
- system-config-time
- system-config-users
- xml2-config
- xmlsec1-config
- xslt-config
- [ /usr/bin ]
复制代码 一样可以凭着印象找出来的,这样也就可以应付所有忘记拼写但使用过的命令的情况了。
我怎么知道一个命令是干什么的?
我们以 ping 命令为例说明:- [ctu@Centos ~]$ man ping
- PING(8) System Manager’s Manual: iputils PING(8)
- NAME
- ping, ping6 - send ICMP ECHO_REQUEST to network hosts
复制代码- [ctu@Centos ~]$ info ping
- File: *manpages*, Node: ping, Up: (dir)
- PING(8) System Manager’s Manual: iputils PING(8)
- NAME
- ping, ping6 - send ICMP ECHO_REQUEST to network hosts
复制代码 PS:man info命令给出了要查找命令的功能说明
我怎么知道一个命令如何使用呢?- [root@Centos ~]# ping
- Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
- [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
- [-M mtu discovery hint] [-S sndbuf]
- [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
复制代码 PS:不带参数执行一个命令,通常会给出该命令的简单使用说明,但不是所有命令都会的- [ctu@Centos ~]$ man ping
- OPTIONS
- -a Audible ping.
- -A Adaptive ping. Interpacket interval adapts to round-trip
- time, so that effectively not more than one (or more, if
- preload is set) unanswered probes present in the net-
- work. Minimal interval is 200msec for not super-user.
- On networks with low rtt this mode is essentially equiv-
- alent to flood mode.
- -b Allow pinging a broadcast address.
- -B Do not allow ping to change source address of probes.
- The address is bound to one selected when ping starts.
- -c count
- Stop after sending count ECHO_REQUEST packets. With
- deadline option, ping waits for count ECHO_REPLY pack-
- ets, until the timeout expires.
- -d Set the SO_DEBUG option on the socket being used.
- Essentially, this socket option is not used by Linux
- kernel.
- -F flow label
- Allocate and set 20 bit flow label on echo request pack-
- ets. (Only ping6). If value is zero, kernel allocates
- random flow label.
复制代码 PS:man命令会给出命令的详细参数说明,还有info也一样,通常info命令会有相应的应用举例,按自己喜好来查找吧
当前目录的可执行文件怎么不能直接执行?
默认情况下,Linux是不搜索当前目录的命令的- [root@Centos ~]# echo $PATH
- /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- [root@Centos ~]# cat -n hp.sh
- 1 #!/bin/bash
- 2
- 3 echo "hello ping"
复制代码 PS:hp.sh作用就是输出“hello ping”- [root@Centos ~]# chmod +x hp.sh
复制代码 PS:添加当前用户的可执行权限- [root@Centos ~]# hp.sh
- -bash: hp.sh: command not found
复制代码 PS:具有可执行权限,也确实存在当前目录下,但是仍然提示命令找不到- [root@Centos ~]# ./hp.sh
- hello ping
复制代码 PS:使用相对路径,我们就可以执行成功- [root@Centos ~]# export PATH=.:$PATH
复制代码 PS:现在我们将当前目录加入到 PATH 环境变量中- [root@Centos ~]# echo $PATH
- .:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
- [root@Centos ~]# hp.sh
- hello ping
复制代码 PS:不使用绝对路径或者相对路径,这次我们也可以执行成功了- [root@Centos ~]# ping
- Usage: ping [-LRUbdfnqrvVaA] [-c count] [-i interval] [-w deadline]
- [-p pattern] [-s packetsize] [-t ttl] [-I interface or address]
- [-M mtu discovery hint] [-S sndbuf]
- [ -T timestamp option ] [ -Q tos ] [hop1 ...] destination
复制代码 PS:此时执行的 ping 命令是系统自带的- [root@Centos ~]# mv hp.sh ping
复制代码 PS:我们将 hp.sh 也改名为 ping- [root@Centos ~]# ping
- hello ping
复制代码 PS:这次执行的ping命令就不再是系统自带的了。命令搜索是按照 PATH 环境变量,以“:”分隔的路径,从左到右逐个搜索的。因此当我们将 hp.sh 脚本改名成 ping 命令时。这时候存在两个 ping 命令了。而当前目录下的 ping 优先于 /bin/ping 命令,也就被执行了。
就写到这里吧,希望大家多多支持!!谢谢!!
--- THE END --- |
|