How To Echo If Variable = something? - cmd

#echo off
set/p=password
if %password%=random echo password correct
How Do I Echo It?
I Keep Getting An Error

Basically the syntay for if is:
if %variable%==content echo blabla
But you should write
if "%variable%"=="content" ....
to prevent a syntax error if the input is empty.

Related

In a bash script what is the difference between ${VAR:-...} and ${VAR:?...}

I'm following this tutorial on transformation of variables.
If I have the following:
echo ${TEST:-test} #TEST is undefined, 'test' is printed and TEST is still undefined.
echo ${FOO:?"some text"} #"some text" is printed and FOO is still undefined.
What is the difference between the ':-' and the ':?' above?
These are testing shortcuts:
echo ${TEST:-test}
If $TEST exists then its value will be used, otherwise the value of $test will be used. If you want TEST to be set then you probably need:
echo ${TEST:=test}
Next one:
echo ${FOO:?"some text"}
If $FOO is set then use its value, else output to stderr the error message "some text" (default is "parameter null or not set").

how to write an empty line in file from a variable?

In Windows Command Line I normally write empty line in a file with
echo; >> file
However, what I have now is a variable
$param1%
If I want echo to write it in the file I have to do
echo %param1% >> file
HERE IS WHERE THE PROBLEM START :
If I'd like an empty like I'd make
set param1=;
However since the ; is not in contact with the echo word the command is
echo ; >> file
which write the ; in the file...
I need the variable to sometime contains text, and sometime nothing. How can I do it?
if "%param1%"=="" echo;>>file else echo %param1%>>file
If a param1 variable does not exist (the same as set "param1="), then %param1% results to:
In a .bat script: %param1% results to an empty string (a string of zero length);
In a CLI window: %param1% results to the %param1% string.
In a .bat script use (note no spaces surrounding %param1%)
>> file (echo;%param1%)
In a CLI window use
>>file (if not defined param1 (echo;) else echo;%param1%)
Note proper using of parentheses in if-else! For instance, check interesting result of next command:
if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
Output:
==>if ""=="" echo;"THEN branch">>file else echo;"ELSE branch">>file
==>type file
"THEN branch" else echo;"ELSE branch"

ambiguous redirect - file gets created

I get an ambiguous redirect message even though the output file gets created.
my sh script
#!/bin/bash
# you can use read or VAR="$1" to setup these variables
SERVER_IP=
SERVER_PORT=
LANGUAGE_URL=
PROJECT_NAME=
while read f1
do
OUTPUTFIL=$f1
{
echo "<?xml version=\"1.0\" encoding=\"Shift-JIS\"?>"
echo "<flash_cfg>"
echo "<server ip=\"${SERVER_IP}\" port=\"${SERVER_PORT}\"/>"
echo "<language_url>${LANGUAGE_URL}</language_url>"
echo "<project_name>${PROJECT_NAME}</project_name>"
echo "</flash_cfg>"
} > ${OUTPUTFIL}
done < file
content of "file
out.xml
while running
:~/Documents$ bash shell.sh
shell.sh: line 22: ${OUTPUTFIL}: ambiguous redirect
The file out.xml is created however
No contradiction there, you have a loop.
So first you read a valid filename (out.xml), and create a file, then you're reading an invalid one, which creates the error message.
Example (you have an empty line in the input):
f=""
echo "Q" > ${f}
-bash: ${f}: ambiguous redirect
I'd use cat to simplify the code--see if this works any better:
while read f1
do
cat <<EOF >"$f1"
<?xml version="1.0" encoding="Shift-JIS"?>
<flash_cfg>
<server ip="${SERVER_IP}" port="${SERVER_PORT}"/>
<language_url>${LANGUAGE_URL}</language_url>
<project_name>${PROJECT_NAME}</project_name>
</flash_cfg>
EOF
done < file
That's known as a "here document" and lets you avoid all those echo's and quoting.

Writing to file shell script

I have been to trying to write some output to a CSV file using the method below in a shell script:
writeToResultFile()
{
resultFile="ShakeoutResult.csv"
msg=" $*"
echo "writing to resultFile..$msg"
echo $msg >> $resultFile
}
When I tried to call this method:
writeToResultFile "column1,column2,column3"
It works fine and was written to output file. But when I tried to call this method from another method such as:
doProcess()
{
writeToResultFile "data1,data2,data3"
}
Nothing is written to the output file. Stepping through, I know that writeToResultFile() is getting invoked and the param also is echoed in the console, but not getting appended to the output file.
Just to make sure: what do you use? Bash? Because it's working:
#!/bin/bash
writeToResultFile() {
msg=" $*"
echo "messaage: $msg"
echo $msg >> output.txt
}
doProcess()
{
writeToResultFile "function1,function2,function3"
}
writeToResultFile "main1,main2,main3"
doProcess
The output will be (cat output.txt):
main1,main2,main3
function1,function2,function3

Shell echo to file ending up on multiple lines?

What I'm doing is: echo put $clientfilepath'client-'$clientversion-'.jar' >> ftp.ftp in a shell file.
Where $clientfilepath is: c:\\workspace\\project\\jack\\prj1\\target\\ and $clientversion is 1.0-snapshot
What I expect in ftp.ftp:
put
c:\\workspace\\project\\jack\\prj1\\target\\client-1.0-snapshot.jar
But what I'm getting is:
put c:\\workspace\\project\\jack\\prj1\\target\\
client-1.0-snapshot
.jar
I'm using double \ so nothing in the filepath should get treated as a special character.
So does anyone know what's happening?
echo put $clientfilepath'client-'$clientversion-'.jar'|tr '\n' '' >> ftp.ftp
You can simplify the quoting:
echo "put ${clientfilepath}client-${clientversion}-.jar" >> ftp.ftp
Try that to see if it helps with your problem. Also try printf instead of echo:
printf 'put %sclient-%s-.jar\n' "$clientfilepath" "$clientversion" >> ftp.ftp

Resources