cd命令為何無法進入xargs管道輸出的目錄

來源:互聯網
上載者:User

今天在寫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://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/

作者:csdn部落格 Jason_asia

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.