Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i use vbscript to write a basic script like in bash for linux

Status
Not open for further replies.

Guy1983

Programmer
Jan 7, 2005
13
US
I want to run a script that just tells the command prompt to execute the dos command "dir" or "cd" etc.

More specifically, I want to use it to execute the rsh command to logon to a remote computer running linux.

"rsh 130.11.232.33 -l lrgs" for example

Does anyone know how to do this?

Thank you

guy
 
Take a look at the WshShell.Run method.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Unless you have a specific reason to use VBScript, this is a very easy thing to do with Windows Shell Scripting.

Create a text file named ls.cmd. Edit it and add the following text:


#Start Script
@ECHO OFF
cd \
cd WINDOWS\System32
dir
pause
#End Script


Save the file and run it. It will do a dir on the \Windows\System32 directory.

You could subsitute your rsh command in place of the dir command. If the rsh command is not in your path you may need to use the entire path to the executable.

Hope this helps...


 
is the disadvantage of using shell scripting that the actual commands and syntax used are specific to the operating system?? where with vbscript the language and syntax etc is the same regardless of what OS you are on??
 
No. The shell script above will work on any Windows or DOS based system. The pathing may differ, but you can get around that using environment variables instead of direct paths.

For example,
I could have said "cd %WINDIR%\System32" instead of "cd WINDOWS\System32"

This would take you to the System32 directory on any Windows box regardless of whether the System32 directory was in the WINNT directory or the WINDOWS directory.

VBScript will only run on systems that have the Windows Scripting Host provider loaded on them. It comes pre-installed on Windows 2000 and above, but must be installed on older systems. So a VBScript would not work on Windows 98 or NT (without WSH) but a shell script would.
 
i see your point about the WSH having to be loaded onto the system. this is the same for most languages, you need runtimes or dlls etc to be there.

am i right in the fact that

IF NOT DEFINED 'shell command??

will not work on pre w2k machines???

my point is that fundamentaly the shell is different on a WXP machine than it is on a W95 machine? or am i wrong?
if it is different then you cant confirm if your script will run all all the OS's because there is no constant dev environment.

with WSH all you need to know is that the WSH environment is installed and you therefore have your constant dev environment across OS's
 
You are correct. The shells are different. However, for the question you originally posted...

["I want to run a script that just tells the command prompt to execute the dos command "dir" or "cd" etc."]

...the differences should not matter. The shell script I gave contains a series of basic commands that are the same across all Windows platforms.

If you are now asking if it is possible to write a shell script that will work on Win95 and not on WinXP. The answer (obviously) is yes, but the same is also true of vbscript using the WScript.Shell object.

For example, if you want to run a shell command (like "dir" or "cd") using vbscript and WSH you have to call the WScript.Shell object. Calling this object simply creates an instance of the shell that is native to the Windows version you are running.

ex)
'START CODE

CONST SVCNAME = "Schedule"
Set ws = CreateObject("WScript.Shell")
ret0 = ws.Run("net start " & SVCNAME,0,"True")
If ret0 <> 0 Then WScript.Echo "There was a problem starting the service: " & SVCNAME

'END CODE

If you are worried about cross-platform viability, you should use vbscript to do some OS detection to run different subroutines based on the different OSs you are planning on supporting.

 
sorry, i wasnt the original poster. i just jumped in cause i was bored and fancied a discussion with a peer about something other than how to get copy a file or whatever :)

i appreaciate the Wscript.Shell is a reflection of the Shell on a client and that they are different for different OS's. this is why personally i would advise against the use of Wscript.Shell to run shell commands for the reasons you have outlined. i believe there is more than enough functionality native to wsh to cover all the commands the shell gives you.

i find it of interest to debate the pros and cons of different approaches, otherwise one forgets what you are really doing!! happens to me all the time :)
 
Hi everyone,

I appreciate all of your responses. They have been helpful. I have used the script that boostadaroosta posted. This is exactly what I needed. However, I cannot get this to run from the command prompt. It does run if I double click it in windows. i have been typing "cscript Is.cmd". Does anyone know how to run the script from the prompt. Thank you.

Guy1983
 
cscript's parameter must be a file with a .VBS extension

To run a .CMD, you just type the name & hit enter. In this case:

c:\>ls.cmd<enter>

when the ls.cmd is in the root of C: or

c:\folder1>ls.cmd<enter>

if it resides in folder1

Optionally, you don't need the '.cmd', you could also just:

c:\>ls<enter>
 
The cmd extension is only executable on NT based windows, the bat one is executable on all windows.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi everyone,

You guys have really helped me out. Thank you. My script does exactly as I want it to. Hopefully, I will be able to answer some of your posts in the future.

Later,

guy1983
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top