Inserting a variable from a batch file into a text file - variables

How would I insert a variable previously established in a batch file into a text file. I have the inserting text into a text file down, i just cant figure out the insertion of a variable.
What I am doing
SET name = "Casey"
ECHO "Hey" + name > file.txt
The result
"Hey" + name
What I want
"Hey Casey"

You should do it like this:
SET name=Casey
ECHO "Hey %name%" > file.txt
Note that there is no spaces before and after the = in
name=Casey

Too bad syntax, you need to forget other programming languages, this is Batch.
First you can't use spaces when assing values to variables, this is the way to do it:
SET "name=Casey"
Also you can do this:
SET "name= Casey"
Second Batch don't have ANY concatenate operator for strings, forget + and &, & is for concatenating commands.
So this is the correct syntax:
SET "name=Casey"
(ECHO Hey %name%)> "file.txt"
Try to use () agrupation operators when Echo a numeric ending string like "MyName 2", to avoid problems in large scripts with Batch redirections.

Related

Pass quoted value sql plus param

I have a shell script script.sh, and a sql script modify_database.sql
In script.sh, I launch the sql script with some param using :
sqlplus user/password ... #modify_database.sql « param_id » « param_newValue »
And in the sql script, I get the parameters value with :
UPDATE T_TEST SET C1 = &param_newValue WHERE ID = &param_id
But I have a lot of problem with the « param_newValue » parameter.
In fact, this parameter could contain some spaces, slash, single quote and double quote character.
For example : L’avion du pilote était surnommé le « Redoutable »
I have a bunch of values to update.
Each new values are stored in a txt file, like that :
id;value
1; L’avion du pilote était surnommé le « Redoutable »
2; Another example of a « test » that’s it
How can I do to pass this param to sqlplus and set the value like that ? The quoted part is giving me a hard time :/
EDIT :
Example, in the input file I have :
123;;;"aaaa/vvvv/COD_039/fff=Avion d'office";"aaaa/vvvv/COD_039/fff=Hello d'orien";
I get this line, I do a cut command to get the first column (ID), the 4th column (to replace), and the 5th column (replacement).
I have a XML node, with a node which contain the 4th column text, and need to replace by the 5th column content.
So I do :
sqlplus -s $DBUSER/$DBPASSWORD#$DBHOST:$DBPORT/$DBSCHEMA #majConfXML.sql "$id" "$newcode"
update T_TEST set XML = updatexml(xmltype(XML_CONF),
'//A[#name="XXX"]//B[#name="YYY"]//pkValue','&2').getClobVal()
But in the param, there is some quotes.
So when I do the update request with '&2', the request is running but it does not work properly.
How can I escape/pass the param value into the updateXML request ?
Thank you
Finally, I find a problem in my bash script before the sqlplus call.
In fact, it was xargs command which I use who interpret simple quote. I simply add double quotes around each string with slash and quote in my input file.
Next, before calling sqlplus, I replace all “ ‘ “ (quote) by “ ‘’ “ (quote quote)

Tcl SQLite update variable substitution cannot have apostrophe

Here's the problem: if I use { } for the update command like so:
package require sqlite3
fileRepo eval {UPDATE uploads SET $col=$data WHERE rowid=$id}
I cannot substitute any variables inside the curly brackets. it all has to be hard coded.
However, if I use " " for the update command like so:
fileRepo eval "UPDATE uploads SET $col='$data' WHERE rowid=$id"
I can substitute variables inside the double quotes, but I must use ' ' in order to put in data with spaces so sql sees it as one input. If I don't I get an error if I send something like
$data = "Legit Stack"
Because it has a space the sql will choke on the word: Stack
unless it is placed within single quotes
Therefore...
If I send this data to the update command:
$col = description
$data = "Stack's Pet"
I get the following error:
near "s": syntax error while executing "fileRepo eval "UPDATE uploads
SET $col='$data' WHERE rowid=$id" ...
Thus given these rules I can see no way to pass a single quote or apostrophe to the update command successfully. Is there a different way to do this?
Thanks!
While it is true that you can escape the single quotes by doubling them (as usual in SQL), you open up your code to the dangers of SQL injection attacks.
It might be better to split your code into two distinct steps:
Substitute with format {UPDATE uploads SET %s=$data WHERE rowid=$id} $col
let sqlite3 magic eval turn the $data and $id into bound variables for a prepared statement
This way you only need to sanitize your col variable, to make sure it contains a valid column name and nothing else (should be easy), instead of all your data. In addition, you do not need to copy large values as often, so a two step approach will even be faster. To make it even clearer you want to use a bind variable, try the alternative syntax with a : in front of a variable name.
package require sqlite3
set stmt [format {UPDATE uploads SET %s=:data WHERE rowid=:id} $col]
fileRepo eval $stmt
Recommended Reading:
For the : syntax: https://www.sqlite.org/tclsqlite.html#eval
For more information about SQL Injections: https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet
You have to use an escape apostrophe. So it should look like this:
$data = "Stack''s Pet"

Batch file; Replace variable's spaces with hyphens

I've started another small Batch project, however I've encountered a bit of a dilemma. I use set name= followed by set /p name=, to allow the user to give the input desired. However, I want the variable entered with spaces to then have the spaces replaced with hyphens (e.g. "hello world" becomes "hello-world").
I've searched around and found many situations in which someone asks a similar question, however they are all using the ren command, which I have no use for due to not dealing with files.
My current code is as follows:
#echo off
Cls
echo Insert a string with spaces?
set string=
set /p string=
I have also found a solution for renaming files that mentioned using ren "!file!" "!file:_= !" to change a file name's spaces to underscores (_). However changing it to set name=!name:-= ! didn't work for me.
How can I best replace spaces with hyphens in my batch file?
You're close.
#echo off
cls
echo Insert a string with spaces?
set string=
set /p string=
set string=%string: =-%
echo String is now %string%
See set /? for details on variable manipulation.

Sqlcmd trailing spaces in output file

Here is my simplified scenario:
I have a table in SQL Server 2005 with single column of type varchar(500). Data in the column is always 350 characters in length.
When I run a select on it in SSMS query editor, copy & paste the result set in to a text file, the line length in the file is 350, which matches the actual data length.
But when I use sqlcmd with the -o parameter, the resulting file has line length 500, which matches the max length of varchar(500).
So question is, without using any string functions in select, is there a way to let sqlcmd know not to treat it like char(500) ?
You can use the sqlcmd formatting option -W to remove trailing spaces from the output file.
Read more at this MSDN article.
-W only works with default size of 256 for variable size columns. If you want more than that you got to use -y modifier which will tell you its mutually exclusive with -W. Basically you are out of luck and as in my case file grows from 0.5M to 172M. You have to use other ways to strip white space post file generation. Some PowerShell command or something.

Adding numbers containing commas in a batch script

I'm trying to add two numbers together in a windows batch file. The numbers are coming from the output of a command and I cannot change the code to output it in a different format.
The problem is that the numbers use commas in the numbers as the thousands separator. i.e. 154022 is output as 154,022. Now when I try to add this number to another number it only adds the first part (i.e. that 154).
set A=1,000
set B=154,022
set /a TOTAL=A + B
echo %TOTAL%
produces: 155, not 155022 that I would like, or even 155,022 would do.
Is there a way to convert easily from numbers with commas to numbers without commas in a batch script?
set A=1,000
set B=154,022
set A2=%A:,=%
set B2=%B:,=%
set /a TOTAL=A2 + B2
echo %TOTAL%
You can do string manipulation like this
set result=%input:substring=replacement%
This one and other nice tips: http://www.dostips.com/DtTipsStringManipulation.php