Introduction to batch scripting - windows

This is my shell script
echo "Name"
read name
if [ "$name" == "abcd" ]; then
echo "correct name"
else
echo "wrong name"
fi
echo "Password"
read password
if [ "$password" == "pwd" ]; then
echo "Correct password"
else
echo "Wrong password"
fi
echo "City"
read city
if [ "$city" == "bangalore" ]; then
echo "correct city"
else
echo "wrong city"
fi
I'm totally new to batch scripting.How do i write an equivalent .bat file?

There are two things you need to read up on:
How to prompt for user input: see the set command and especially set /p. You can get information about this by typing help set at the prompt.
How to test string equality: The if command has some info on this, type help if at the prompt to get some details.
When you have these things in place, you should be able to replicate the behavior of your bash script.

Have a look here for how to get input:
Batch File Input
Edit: Link contents (no longer available) was:
The following method will work on Windows 2000 and XP:
set INPUT=
set /P INPUT=Type input: %=%
echo Your input was: %INPUT%
If user hits ENTER without typing anything, the variable (in this case, %input%) keeps it value. So, the first line is to reset its value, so that if nothing is entered the variable will have no value at all, instead of some senseless value.
As you can see, that accepts blank inputs. You can make it to don't accept:
:input
set INPUT=
set /P INPUT=Type input: %=%
if "%INPUT%"=="" goto input
echo Your input was: %INPUT%
So, after getting user's input and saving it in a variable, you can use it as you will.

Consider running bash script on windows instead of porting script. See bash-shell-for-windows. It might be easier approach.

Related

Cant use default option more than one time(s)

i am trying to use the "timeout" command in a batch script, and it says "ERROR: Invalid syntax. Default option is not allowed more than one time(s)" and it closes the script after.
Can anyone help me fix this? The code is as follows
#echo off
title {REDACTEDPRIVATESTACKOVERFLOW}
echo {REDACTEDPRIVATESTACKOVERFLOW}
timeout /t 3 /timeout
echo {REDACTEDPRIVATESTACKOVERFLOW}
It just worked for me in the following way:
echo "hi"
timeout 3
echo "hi"

Pass user inputs from one script to another during runtime

I have a requirement where ScriptA.sh has commands to ask for User's inputs and perform a set of actions. I want to automate this by creating another script which will read the questions asked from output of ScriptA.sh and provide the necessary values in runtime.
ScriptA.sh as follows :-
echo "Enter the CR Number"
read varnamecr
echo "CR Number is" $varnamecr
echo "Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR"
read optionchoosen
echo "Option Choosen is :" $optionchoosen
echo "Will run the script/load configuration is this Ok ?[y/N]"
read userinput
echo "Proceed further, User has pressed ->"$userinput"<--Key"
How to write the second script to achieve this. Tried spawn and few other commands in the second script, but no luck. Please help me with this.
Since you're not specifying any shell in your tag, this is a possible, albeit crude, solution in ksh. It's using the coprocess capability of that shell (pretty sure it's not supported in bash although please don't quote me on that one)
#!/bin/ksh
./ScriptA.sh |&
while read -p Dummy; do
print $Dummy
case $Dummy in
"Enter the CR Number")print -p "CR123456"
;;
"3.MAR")print -p "3"
;;
"Will run the script"*)print -p "y"
;;
esac
done
The output gives :
Enter the CR Number
CR Number is CR123456
Loading the config set. Choose Option From Below set
1.JAN
2.FEB
3.MAR
Option Choosen is : 3
Will run the script/load configuration is this Ok ?[y/N]
Proceed further, User has pressed ->y<--Key
Will input remain same everytime? If so you can create wrapper of this script to provide required input.
cat wrapper
./ScriptA.sh <<!
123
2
y
!

Passing parameter from window bat to UNIX shell script

i know topic seems very familiar and there are many answers given for same, but my issue is something else.I have a .bat file in which i am passing more then 10 parameters(I know limit is 9 but i need it).It seems to be working fine.Script is below-:
#ECHO OFF
setlocal EnableDelayedExpansion
set n=0
for %%a in (%*) do (
set vector[!n!]=%%a
set /A n+=1
)
SET sadminUser=%vector[0]%
SET sadminPassword=%vector[1]%
SET dsnProvider=%vector[2]%
SET dbUserName=%vector[3]%
SET schemaFilePath=%vector[4]%
SET ddldictschemalogPath=%vector[5]%
SET repositoryName=%vector[6]%
SET ddlimpschemalogPath=%vector[7]%
SET grantUserRole=%vector[8]%
SET userId=anyuser
SET host=myhost
SET password=password
SET datatable=%vector[9]%
SET indexTable=%vector[10]%
SET ddlimpschemalogPath2=%vector[11]%
echo %ddlimpschemalogPath2%
echo y | "C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% exit
"C:\Program Files\PuTTY\plink" -ssh %userId%#%host% -pw %password% "PATH=/bin:/usr/bin:/usr/local/bin /opt/siebel/w44gq8sw/DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
Now as you see i am doing echo %ddlimpschemalogPath2% which is last parameter in array,and i can see correct output as well.
Problem is when i try to pass these parameters into UNIX Shell Script.You can see i am doing in bat file using putty command line Plink.I can successfully connect to shell script as well,And i am trying to echo all the passed parameters in shell script.But facing some issue . Script is below-:
#!/bin/bash
sadminUser=$1
sadminPassword=$2
dsnProvider=$3
dbUserName=$4
schemaFilePath=$5
ddldictschemalogPath=$6
repositoryName=$7
ddlimpschemalogPath=$8
grantUserRole=$9
datatable=${10}
indexTable=${11}
ddlimpschemalogPath2=${12}
echo "$sadmimUser"
echo "$sadminPassword"
echo "$ddlimpschemalogPath2"
echo password | sudo -S -l
sudo host << EOF
// Do something else
EOF
I Found one issue. In .bat i have parameter called -: repositoryName . When i do echo %repositoryName% it gives "Siebel Repository" correct output. Now when this spaced value is passed as parameter to shell script it breaks into 2 different values so:
These two parameter in script take 2 different values-:
repositoryName=$7
ddlimpschemalogPath=$8
Output-:
Siebel
repository
And it should be one value for parameter repositoryName=$7 . Giving value Siebel Repository. Why is this happening? Can this me issue why value are up and down?
As you can see in .bat i am making call to shell script, and passing parameters taken from .bat file as below-:
DDL_Sync.sh %sadminUser% %sadminPassword% %dsnProvider% %dbUserName% %schemaFilePath% %ddldictschemalogPath% %repositoryName% %ddlimpschemalogPath% %grantUserRole% %datatable% %indexTable% %ddlimpschemalogPath2%" > ddlSuccess.txt 2>&1
You can see complete line above in .bat.
Invoking .bat file -:
dlSync.bat "SAD" "glob81" "gepf_DSN" "SIEBEL" "/global/u70/globepfdev/siebel/schema.ddl" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/expschem.log" "Siebel Repository" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync1.log" "SSE_ROLE" "GLOB_DATA_SMALL" "GLOB_INDEX_SMALL" "/global/u70/globepfdev/siebel/siebsrvr/log/dev2prod/output/ddlsync2.log"
Same parameter are passing out to shell script . where "Siebel Repository" breaks into 2. Thanks

bash dialog inputbox with variable entered

I'm struggling with this code:
komanda=$(dialog --title "COMMAND" --backtitle "ENTER COMMAND: "
--inputbox "" 8 180 2>&1 >/dev/tty)
if [ $? == 0 ] then
for ((j=0;j<$tlen;j++))
do
shopnum=${selectedRPODS[j]}
$komanda #executing the entered comand with variables.
done
fi
When I enter a variable in the inputbox, the parameter is passed on as a string. Variables are not working when execute at $komanda
Example input text:
ping $shopnum
returns always:
ping: unknown host $shopnum
I wanted to have the variable as destination for the ping. I know its something I'm doing wrong with the STDOUT but I can't find the right way to do it.
Thanks in advance.
Parameter expansions are not recursive. If you enter ping $shopnum, then $komanda expands to the literal text ping $shopnum, not ping 3 or whatever value was assigned to shopnum.
In this case, since your intention is to execute arbitrary shell code, you would use eval:
eval "$komanda"
However, it would be better to rethink your design so that the user doesn't need to know what variable names exist in the code.

How to pass a variable into %0 part of batch file with ftp?

I have a batch file I want to be able to call from the command line like:
myBatch.bat testParam
That batch file is the following:
set sid=%1
C:\Windows\System32\ftp.exe -s:%0
goto done
open servername
username
password
get 'FilePath%sid%restoffilepath' targetPath\%sid%MyName.txt
bye
:done
However, I cannot seem to get the FilePath%sid%restoffilepath part to work right - I believe it is because the %0 is treating the input as literal, but I'm not 100% sure. The %sid% variable is not expanded.
I basically want to have FilePath%sid%restoffilepath be FilePathtestParamrestoffilepath in this case.
Think carefully about what you are doing here--the ftp.exe is reading the file. Your batch script, which knows what %1 is, is not feeding the data to ftp.exe.
What you need to do is to shoot the script out to a file, and then run the ftp command:
set sid=%1
echo open servername >> myftp.txt
echo username >> myftp.txt
echo password >> myftp.txt
echo get 'FilePath%sid%restoffilepath' targetPath\%sid%MyName.txt >> myftp.txt
echo bye >> myftp.txt
C:\Windows\System32\ftp.exe -s:myftp.txt

Resources