2014-11-14 107 views
-3

我已創建一個.bat文件,然後我將其轉換爲.exe以編輯Windows系統上的hosts文件。我正在接受一個shell腳本。.BAT等效於MAC

我的.bat腳本是

@ECHO off 
setlocal enabledelayedexpansion 
::Create your list of host domains 
set LIST=(www.google.com) 
::Set the ip of the domains you set in the list above 
set www.google.com=192.168.1.1 
:: deletes the parentheses from LIST 
set _list=%LIST:~1,-1% 
::ECHO %WINDIR%\System32\drivers\etc\hosts > tmp.txt 
for %%G in (%_list%) do (
    set _name=%%G 
    set _value=!%%G! 
    SET NEWLINE=^& echo. 
    ECHO Carrying out requested modifications to your HOSTS file 
    ::strip out this specific line and store in tmp file 
    type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_name! > tmp.txt 
    ::re-add the line to it 
    ECHO %NEWLINE%^!_value! !_name!>>tmp.txt 
    ::overwrite host file 
    copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts 
    del tmp.txt 
) 
ipconfig /flushdns 
ECHO. 
EXIT 

我曾嘗試以下http://tldp.org/LDP/abs/html/dosbatch.html但沒有成功。

+0

雖然你在暗指腳本文件,但這更多的是shell/OS問題,屬於[su]。如果您在實際編寫腳本時遇到問題,則該網站將是適當的。 – 2014-11-14 15:12:10

+0

我在編寫腳本時遇到了問題,我不知道如何使用或使用哪種腳本語言。謝謝。 – 2014-11-14 15:20:29

回答

2

那麼MacOS是一個基於Unix的系統,所以你可以使用任何shell命令。請在Internet上查找「如何使用Bash腳本」或「Sh Script」

+0

感謝您的有用信息! – 2014-11-14 15:19:16

1

修改主機文件可以在Mac OS X上使用shell腳本完成。如果希望用戶能夠像執行簡單腳本一樣執行shell腳本儘可能(即通過雙擊一個圖標),您可能需要考慮使用AppleScript來執行shell腳本。 AppleScript可以存儲爲可執行文件,以便輕鬆傳遞。

+0

非常感謝您的幫助 – 2014-11-14 15:19:43