歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux維護 >> 怎麼解決Linux cd命令進入xargs管道輸出目錄報錯的問題

怎麼解決Linux cd命令進入xargs管道輸出目錄報錯的問題

日期:2017/3/2 10:35:56   编辑: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的原理做深入研究。

上面就是cd命令無法進入xargs管道輸出的目錄的解決方法的介紹了,因為xargs牽涉寫管道,而cd是內部命令,所以遇到這種情況的時候修改命令就可以進入指定的目錄了,你了解了嗎?

Copyright © Linux教程網 All Rights Reserved