四时宝库

程序员的知识宝库

shell 查找可执行文件(路径)(第二个脚本)

查找可执行文件

  #$PATH 为系统内置变量
[root@data2 testsh]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

#脚本内容 
[root@data2 testsh]# cat search_shell.sh 
#!/bin/bash
# 查找path中的可执行文件
#分隔符重定义
IFS=:
# 循环path,以:分割的目录
for dir in $PATH
do
        # 循环目录下的文件
        for file in $dir/*
        do
                # 判断文件是否有可执行权限,如果有,输出打印,最后保存到文件shell_file.txt 
                if test -x $file
                then
                        echo "  $file"
                fi
        done 
done >shell_file.txt 

添加执行权限

[root@data2 testsh]# chmod 755 search_shell.sh

[root@data2 testsh]# ll

总用量 4

-rwxr-xr-x. 1 root root 351 1月 22 12:27 search_shell.sh

执行并查看

[root@data2 testsh]# ./search_shell.sh

[root@data2 testsh]# ll

总用量 32

-rwxr-xr-x. 1 root root 351 1月 22 12:27 search_shell.sh

-rw-r--r--. 1 root root 25121 1月 22 12:35 shell_file.txt



发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接