歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux系統中cd無法進入xargs管道輸出的目錄

Linux系統中cd無法進入xargs管道輸出的目錄

日期:2017/3/1 17:57:17   编辑:Linux技術

今天在寫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的原理做深入研究。

【參考鏈接】

http://bbs.chinaunix.net/thread-750335-2-1.html
http://blog.csdn.net/jason_asia/article/details/28407189

Copyright © Linux教程網 All Rights Reserved