LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-06-2007, 11:52 AM   #1
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Rep: Reputation: 30
Question 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
thanks,

Last edited by Dr_Death_UAE; 09-06-2007 at 11:53 AM.
 
Old 09-06-2007, 12:28 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Install the gnuwin32 package(s) on your Windows system, edit the path settings to include the executables, and use your Linux script.
 
Old 09-06-2007, 12:49 PM   #3
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
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
 
Old 09-06-2007, 02:02 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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]

Last edited by PTrenholme; 09-06-2007 at 02:07 PM.
 
Old 09-06-2007, 02:11 PM   #5
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
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.
 
Old 09-06-2007, 03:10 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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?
 
Old 09-06-2007, 03:37 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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;
}
 
Old 09-06-2007, 07:02 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
there's no need to install anything.
just use the for loop. See examples here point 4.
 
Old 09-07-2007, 08:34 AM   #9
Dr_Death_UAE
Member
 
Registered: Jul 2005
Location: U.A.E
Distribution: FreeBSD,Fedora,Solaris,AIX
Posts: 168

Original Poster
Rep: Reputation: 30
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.
 
Old 09-07-2007, 11:05 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by Dr_Death_UAE View Post

for loop will not set the output command as a variable.
have you tried the for loop? where is it in your code?
 
Old 09-09-2007, 07:18 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.

Last edited by PTrenholme; 09-09-2007 at 07:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Running Windows batch script from Unix rrguyyala AIX 4 08-26-2007 08:43 AM
tcsh set/read variable command? johnpaulodonnell Linux - Newbie 2 08-22-2007 09:26 AM
How to set PATH variable for command tracert? princegbenga Linux - Software 2 06-14-2007 07:48 AM
set the ouput of a command to a variable true_atlantis Linux - Newbie 2 03-23-2006 03:30 PM
how do you set an env variable from within a csh script? BrianK Linux - General 3 05-26-2004 02:16 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration