iT邦幫忙

2024 iThome 鐵人賽

DAY 15
0
佛心分享-我的私藏工具箱

作業系統的專武系列 第 15

用DOS寫個過版Script

  • 分享至 

  • xImage
  •  

後來覺得作業系統的專武,其實早有一個正式名稱,叫CLI,如jboss-cli、redis-cli等,Windows的CLI雖然是PowerShell,但仍保留支援前一版的CLI:DOS Script。而在系統在客戶端或公司VM的主機行之有年,年紀可能都能念國中、高中了,不支援太新版的PowerShell,DOS Script還是能加減用。
以下是當初幫客戶寫的過版Tableau的Script,需要輸入Tableau帳密及DB的密碼再進行過版。

@echo off
SetLocal EnableExtensions EnableDelayedExpansion
set /p id="Input TABLEAU Account:"
set /p pwd="Input TABLEAU Password:" < Nul
call :PasswordInput

set /p pwd="Input DB Password:" < Nul
call :PasswordInput2
for %%a in (F:\publish\*) do (
    set filename=%%a
    echo !filename!
    set tt=!filename:~11,-4!
    echo !tt!
    del !tt!.log
    "F:\Tableau\Tableau Server\10.5\bin\tabcmd" publish !filename! -n !tt! -s http://127.0.0.1:8000 --username %id% --password !LINE! --db-username RPTUser --db-password !LINE2! --project "RPTPRJ" --save-db-password --save-oauth --overwrite --tabbed 2>!tt!.log
    call :filesize ".\!tt!.log"
rem    echo !size!
    if !size! GTR 0 (
        echo Deploy Error
        goto :Eof
    )
)

Goto :Eof

:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd

:PasswordInput2
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line2 a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line2="
:_PasswordInput_Kbd2
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line2 (Set /P "=!BS! !BS!" <Nul
Set "Line2=!Line2:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line2=!Line2!^!"
) Else Set "Line2=!Line2!!CHR!"
)
Goto :_PasswordInput_Kbd2

:: set filesize of 1st argument in %size% variable, and return
:filesize
set size=%~z1
exit /b 0
  • 當時最難處理的一塊是密碼要用遮罩,在set /p pwd="Input TABLEAU Password:" < Nul後接著是call :functionname,call後面以往是呼叫外部程式,這裡呼叫:functionname則是指同支Script內的Label,也就是GOTO,年輕人應該很少知道早年GOTO是寫程式大忌了。至於PasswordInput_Kbd這支程式是怎麼把密碼遮罩起來,坦白說太複雜我也沒研究,能用就好。
  • !filename:~11,-4!這一段是字串截取,用驚嘆號括首尾,從第11碼起截至倒數第4碼。在Windows裡,%variable%相當於Unix Like的$variable。然而驚嘆號,!epxressoin!相當於Unix Like的${expression}

上一篇
判斷作業系統是32還是64 bits
下一篇
用Script和Command證明升級後沒再報錯。
系列文
作業系統的專武30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言