如何解决Linux cd命令进入xargs管道输出目录报错的问题


最近有网友在书写shell脚本的时候,发现使用cd命令无法进入xargs管道输出的目录,出现报错的情况,那么遇到这种情况可以使用修改命令来解决,一起来了解下具体的操作吧。

【环境描述】

目录结构:

[root@test1 nagiosclient_db]# ls -ltr

total 2488

-rw-r--r--。 1 root root 405725 Mar 3 14:12 nrpe-2.12.tar.gz

-rw-r--r--。 1 root root 2095419 Mar 3 14:12 nagios-plugins-1.4.15.tar.gz

drwxrwxrwx. 16 501 root 4096 Mar 3 14:36 nagios-plugins-1.4.15

drwxrwxr-x. 7 500 500 4096 Mar 3 14:37 nrpe-2.12

获取需要的目录:

[root@test1 nagiosclient_db]# ls | egrep ‘nrpe-[0-9]。[0-9]+.$’

nrpe-2.12

使用管道进入指定的目录:

[root@test1 nagiosclient_db]# ls | egrep ‘nrpe-[0-9]。[0-9]+.$’ | xargs ls -ld | xargs cd

xargs: cd: No such file or directory

遇到报错提示。

修改命令:

[root@test1 nagiosclient_db]# cd `ls | egrep ‘nrpe-[0-9]。[0-9]+.$’ `

[root@test1 nrpe-2.12]# pwd

/home/monitor/nagiosclient_db/nrpe-2.12

进入了指定的目录。

【为什么cd命令不能进入xargs管道输出的目录】

xargs牵涉写管道,而cd是内部命令。具体的牵涉shell的工作原理。

查看cd命令的绝对路径:

[root@test1 nrpe-2.12]# which cd

/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)

查看xargs的绝对命令:

[root@test1 nrpe-2.12]# which xargs

/usr/bin/xargs

详情如上,因非系统工程师,故此处不对shell的原理做深入研究。

上面就是cd命令无法进入xargs管道输出的目录的解决方法的介绍了,因为xargs牵涉写管道,而cd是内部命令,所以遇到这种情况的时候修改命令就可以进入指定的目录了,你了解了吗?


上一篇:Linux软件如何安装及管理指南

下一篇:Linux下如何处理一次用户态进程死循环问题


Linuxxargs Linuxcd命令 xargs管道
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器