Supersonic
Supersonic

Reputation: 430

Execute few command line bash commands in ruby

Have a small bash script which I would like to put it into a method in ruby the script content is as follows.

def test_method
  `@echo off`
  `SETLOCAL ENABLEDELAYEDEXPANSION`
  `SET LinkName=Projects`
  `SET Esc_LinkDest=%userprofile%\Links\!LinkName!.lnk`
  `SET Esc_LinkTarget=C:\Projects`
  `SET cSctVBS=CreateShortcut.vbs`
  `SET LOG=".\%~N0_runtime.log"`
  `((`
    `echo Set oWS = WScript.CreateObject^("WScript.Shell"^)`
    `echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)`
    `echo Set oLink = oWS.CreateShortcut^(sLinkFile^)` 
    `echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)`
    `echo oLink.Save`
    `)1>!cSctVBS!`
  `cscript //nologo .\!cSctVBS!`
  `DEL !cSctVBS! /f /q`
  `)1>>!LOG! 2>>&1`
end

It executes till (( i.e line number 8 and gives me the error No such file or directory - (( That part in the bash script is something which I am writing some content to a file.

Just want to execute that method and make it work like if I had written the same content in a bat file it works absolutely fine.

Upvotes: 0

Views: 124

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You are trying to execute each single line of your script, what is wrong. Just prepare your script and then execute it all in once.

cmd = <<EOC
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET LinkName=Projects
SET Esc_LinkDest=%userprofile%\Links\!LinkName!.lnk
SET Esc_LinkTarget=C:\Projects
SET cSctVBS=CreateShortcut.vbs
SET LOG=".\%~N0_runtime.log"
((
echo Set oWS = WScript.CreateObject^("WScript.Shell"^)
echo sLinkFile = oWS.ExpandEnvironmentStrings^("!Esc_LinkDest!"^)
echo Set oLink = oWS.CreateShortcut^(sLinkFile^) 
echo oLink.TargetPath = oWS.ExpandEnvironmentStrings^("!Esc_LinkTarget!"^)
echo oLink.Save
)1>!cSctVBS!
cscript //nologo .\!cSctVBS!
DEL !cSctVBS! /f /q
)1>>!LOG! 2>>&1
EOC

# OK, time to rock’n’roll!
`#{cmd}`

Upvotes: 2

Related Questions