LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Windows batch script: SET command as variable (https://www.linuxquestions.org/questions/programming-9/windows-batch-script-set-command-as-variable-582710/)

Dr_Death_UAE 09-06-2007 11:52 AM

Windows batch script: SET command as variable
 
Hello,

how can I set a command as a variable in windows batch script?

this is the script //its just a test:

Code:

@ECHO off
SET /P userpass=Enter Password:
::hash = 123
SET userhash=202CB962AC59075B964B07152D234B70
SET userpass1=%cd%md5.exe -d%userpass%

IF "%userhash%" == "%userpass1%" (       
        ECHO good
) ELSE (
        ECHO bad
)
goto :EOF

in linux it accept commands with variables:

Code:

#!/bin/sh
echo -n "Enter Password: "
read userpass
#hash = 123
userhash="ba1f2511fc30423bdbb183fe33f3dd0f"
userpass1=`echo "$userpass" | md5sum | awk '{print $1}'`

if [ "$userhash" == "$userpass1" ]; then
        echo "good"
else
        echo "bad"
fi


any idea :scratch:
thanks,

PTrenholme 09-06-2007 12:28 PM

Install the gnuwin32 package(s) on your Windows system, edit the path settings to include the executables, and use your Linux script.

Dr_Death_UAE 09-06-2007 12:49 PM

Hi, Linux is r0x and there is no compare between Windows and Linux.

But I'm writing a batch script and I need to make it run.

Thanks

PTrenholme 09-06-2007 02:02 PM

That's why I suggested that you install the ported Linux utilities on your Windows system -- so you can run a Linux shell script on WinDoze.

Granted, the basic DOS command system was a port of the UNIX scripting language, it (the "cmd" shell) has not matured to match even the old Bourne shell.

If you're really "stuck" with cmd, consider spawning the command, piping the output to a file, and then reading the output in your script. [edit]
One trick I often used was to pass the output through a stream editor to create the desired SET commands, and to then call the file from the parent script. Not eligant, but it does work.
[/edit]

Dr_Death_UAE 09-06-2007 02:11 PM

PTrenholme, I have cygwin on my windows box, but the script i want it to run not just on my box.

thats the problem, i always write my scripts in Linux shell script. but for this one i need it to be in windows batch script.

PTrenholme 09-06-2007 03:10 PM

So? Why can't you distribute cygwin or gnuwin32 with your script?

And is there some reason you can't use the other suggestion I offered?

PTrenholme 09-06-2007 03:37 PM

Here's a simple example of what I did (in Widows 95 -- It's been a while) to create a "cwd" batch command:

First, the batch file to build the batch file:
Code:

@echo off
echo @echo off> f:cwd.bat
cd | make_wd >> f:cwd.bat
cd

and then the "make_cd" code (C source):
Code:

#include <stdio.h>
#include <stdlib.h>
void main()
{
  register int c;
  while ((c = getchar()) != EOF) {
    switch ((char) c) {
      case ':': {
        (void) putchar(c);
        (void) putchar((int) '\n');
        (void) fputs("cd ", stdout);
        break;
      }
      case '\n': {
        break;
      }
      default: {
        (void) putchar(c);
        break;
      }
    }
  }
  (void) putchar((int) '\n');
  return;
}


ghostdog74 09-06-2007 07:02 PM

there's no need to install anything.
just use the for loop. See examples here point 4.

Dr_Death_UAE 09-07-2007 08:34 AM

Hi, this script i wrote it to set password for USB flash memory, the password will be stored in the batch file as a clear password, so i use md5sum to compare the hash with the user input.

this is version 0.1, where the password is clear text:

Code:

@ECHO off
::Coded By Dr.Death 2007 V0.1
COLOR 0C
TITLE = -- L O S T  - A N D -  F O U N D --
ECHO.
ECHO This Flash Disk belong to <YourName>, If you Found it please call "<Phone #>"
ECHO.
                pause
ECHO.
        :BEGINLOOP
                                                ECHO .::Please Enter Password::.
ECHO.
SET /P UserPass=Enter Password:
        IF %UserPass% == password (
 ECHO Login Succeeded
                explorer %cd%

        ) ELSE (
                                ECHO Access Denied
                GOTO :BEGINLOOP
        )

GOTO :EOF

this is why i need it to be in windows batch script.

for loop will not set the output command as a variable.

ghostdog74 09-07-2007 11:05 AM

Quote:

Originally Posted by Dr_Death_UAE (Post 2884384)

for loop will not set the output command as a variable.

have you tried the for loop? where is it in your code?

PTrenholme 09-09-2007 07:18 PM

This is somewhat off-topic to your original question, but, if you're trying to protect the contents of your flash memory from unauthorized access, wouldn't you be better off just using an encrypted file system on the flash memory?

If, for some obscure reason, you want to stick with Windows, you can easily "simulate" an encrypted file system by making all on the flash memory you wish to protect an encrypted ZIP file, which will require a password for access. (I haven't tried it, but I think that Windows Explorer will request the P/W when you &quot;ckick&quot; on the ZIP file.)

It seems to me that your scheme assumes that whoever finds the flash memory will "boot" the device, not simply mount it and access it's contents. That scenario seems, to me, to be fairly improbable. Who in their right mind would actually boot an unknown device on their system?

P.S. -- I bought a USB flash drive a while ago, and it was delivered with a FAT file system installed. But Fedora was quite happy to reformat as an ext3 file system for me, and installing an encrypted file system on it -- if I wanted to do so -- would probably be quite easy.


All times are GMT -5. The time now is 05:52 AM.