我正在嘗試以下操作.. 系統「cd目錄文件夾」 但它失敗,我也嘗試系統「退出」離開終端,但它失敗。如何使用perl cd進入目錄?
4
A
回答
22
代碼:
chdir('path/to/dir') or die "$!";
的Perldoc:
chdir EXPR
chdir FILEHANDLE
chdir DIRHANDLE
chdir Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by $ENV{HOME}, if set; if not, changes to
the directory specified by $ENV{LOGDIR}. (Under VMS, the variable
$ENV{SYS$LOGIN} is also checked, and used if it is set.) If neither is set,
"chdir" does nothing. It returns true upon success, false otherwise. See the
example under "die".
On systems that support fchdir, you might pass a file handle or directory
handle as argument. On systems that don't support fchdir, passing handles
produces a fatal error at run time.
14
你無法通過調用system
做這些事情的原因是system
將開始一個新的進程,執行你的命令,並返回退出狀態。所以當你打電話給system "cd foo"
時,你將開始一個shell進程,它將切換到「foo」目錄,然後退出。在你的perl腳本中沒有任何後果發生。同樣,system "exit"
將啓動一個新進程並立即退出。
你想要的CD盒,是 - 正如bobah指出的那樣 - 功能chdir
。退出您的程序,有一個功能exit
。
但是 - 這些都不會影響您所在終端會話的狀態。在您的perl腳本完成後,終端的工作目錄將與啓動之前的工作目錄相同,您將無法退出終端會話通過在您的perl腳本中調用exit
。
這是因爲你的perl腳本又是一個獨立於你的終端shell的進程,而在單獨的進程中發生的事情通常不會互相干擾。這是一個功能,而不是一個錯誤。
如果您希望在您的shell環境中更改內容,則必須發出您的shell可以理解和解釋的指令。 cd
在你的shell中就是這樣的內置命令,就像exit
一樣。
3
我總是喜歡提File::chdir
爲cd
-ing。它允許更改封閉塊本地的工作目錄。
正如Peder所言,您的腳本基本上都是與Perl系統調用綁定在一起的。我提出了更多的Perl實現。
"wget download.com/download.zip";
system "unzip download.zip"
chdir('download') or die "$!";
system "sh install.sh";
變爲:
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::Simple; #provides getstore
use File::chdir; #provides $CWD variable for manipulating working directory
use Archive::Extract;
#download
my $rc = getstore('download.com/download.zip', 'download.zip');
die "Download error $rc" if (is_error($rc));
#create archive object and extract it
my $archive = Archive::Extract->new(archive => 'download.zip');
$archive->extract() or die "Cannot extract file";
{
#chdir into download directory
#this action is local to the block (i.e. {})
local $CWD = 'download';
system "sh install.sh";
die "Install error $!" if ($?);
}
#back to original working directory here
這裏使用兩個非核心模塊(和Archive::Extract
只被核心因爲Perl v5.9.5),所以你可能需要安裝它們。使用cpan
實用程序(或AS-Perl上的ppm
)執行此操作。
相關問題
- 1. cd進入目錄,而無需許可
- 2. Crontab CD到目錄
- 3. 使用bash更改目錄'cd'
- 4. 如何更改目錄使用Perl
- 5. Sencha命令 - 不是有效的sdk目錄,請cd進入有效的目錄
- 6. 如何在「grep」之後「cd」到目錄?
- 7. 如何cd到目錄echo'd腳本
- 8. 如何進入特定目錄(cd)並通過java執行bash命令(python server)?
- 9. 如何使用PowerShell別名cd spec規格目錄?
- 10. 如何使用Ganymed SSH API更改目錄(cd)?
- 11. sudo cd到織物目錄
- 12. 如何將`cd'放入名爲`-`的目錄中?
- 13. 如何遍歷目錄和cd到每個目錄
- 14. 使用Perl在目錄
- 15. 使用ICDBurn刻錄CD
- 16. 如何進入CMD的子目錄?
- 17. 你如何通過命令調用終端並cd到目錄?
- 18. 我如何使用腳本進入目錄?
- 19. 進入JSON目錄?
- 20. 如何遍歷一個目錄,而不進入子目錄
- 21. 如何使用bean進入android項目?
- 22. 如何使用StringTemplate導入目錄?
- 23. 在iOS應用上使用libssh2更改目錄「cd」
- 24. 如何打開目錄並使用perl讀取該目錄內的文件
- 25. Powershell:進入系統目錄?
- 26. SVN加入目錄修改的內容沒有CD
- 27. cd從父母的子目錄
- 28. ssh2_exec()不會改變目錄與「cd」
- 29. cd不能在目錄上工作
- 30. 使用perl的TokeParser更改目錄
我輸入了'chdir('folder01')或者'$!';''我的解壓縮行後,但我得到以下錯誤。 語法錯誤it.pl第6行,靠近「系統」 執行it.pl因編譯錯誤而中止。 – sirplzmywebsitelol
@sirplzmywebsitelol你的「解壓縮行」在這種情況下沒有意義。你可以用一個或多或少的完整的代碼片段來更新你的問題,所以我們可以看到你想要做什麼? –
system「wget http://download.com/download.zip」 system「unzip download.zip」 chdir('download')or die「$!」; 系統「sh install.sh」; – sirplzmywebsitelol