error when passing string argument from batch to vbs - string

I'm calling a vbs string with a batch file. I'm passing a string through batch to vbs.
Complete batch file:
C:
cd C:\folder
Set arg = "sample foo"
Wscript titi.vbs "%arg"
pause
But, when I read the argument into the VBScript with str = Wscript.Arguments(0) the value of str is sample and not to sample foo
How can I fix it?

The problem is a simple one and one which is commonly made by those having used other programming languages.
Set arg = "sample foo"
sets the variable: %argspace% to the string: space"sample foo"
The way to assign variables should be:
Set arg="sample foo"
or:
Set "arg="sample foo""
I prefer the latter.
BTW, you also missed a closing % when using "%arg" instead of "%arg%".
Because you are using the argument as "%arg%" there is no neeed to set the double quotes into the actual variable string.
Just use:
CD /D "C:\folder"
Set "arg=sample foo"
Wscript titi.vbs "%arg%"
Pause

I tried below and working as expected,
1.vbs
str = Wscript.Arguments(0)
WScript.Echo(str)
1.bat
Wscript 1.vbs "sample foo"
cmd line output

The batch file you posted should have the VBScript output arg due to your malformed variable syntax. Even if you corrected %arg to %arg% you should get an empty string as output, because your variable assignment is malformed as well (set arg =... defines a variable %arg %).
Change
Set arg = "sample foo"
Wscript titi.vbs "%arg"
to
set "arg=sample foo"
Wscript titi.vbs "%arg%"
and the problem will disappear.

Related

How do I assign a File into a variable to work with that variable? [duplicate]

I have this script called test.sh:
#!/bin/bash
STR = "Hello World"
echo $STR
when I run sh test.sh I get this:
test.sh: line 2: STR: command not found
What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and this is how they say to declare variables... So I'm not sure what I'm doing wrong.
I'm on Ubuntu Server 9.10. And yes, bash is located at /bin/bash.
You cannot have spaces around the = sign.
When you write:
STR = "foo"
bash tries to run a command named STR with 2 arguments (the strings = and foo)
When you write:
STR =foo
bash tries to run a command named STR with 1 argument (the string =foo)
When you write:
STR= foo
bash tries to run the command foo with STR set to the empty string in its environment.
I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:
the first command is exactly equivalent to: STR "=" "foo",
the second is the same as STR "=foo",
and the last is equivalent to STR="" foo.
The relevant section of the sh language spec, section 2.9.1 states:
A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.
In that context, a word is the command that bash is going to run. Any string containing = (in any position other than at the beginning of the string) which is not a redirection and in which the portion of the string before the = is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo", STR is not a variable assignment.
Drop the spaces around the = sign:
#!/bin/bash
STR="Hello World"
echo $STR
In the interactive mode everything looks fine:
$ str="Hello World"
$ echo $str
Hello World
Obviously(!) as Johannes said, no space around =. In case there is any space around = then in the interactive mode it gives errors as
No command 'str' found
I know this has been answered with a very high-quality answer. But, in short, you cant have spaces.
#!/bin/bash
STR = "Hello World"
echo $STR
Didn't work because of the spaces around the equal sign. If you were to run...
#!/bin/bash
STR="Hello World"
echo $STR
It would work
When you define any variable then you do not have to put in any extra spaces.
E.g.
name = "Stack Overflow"
// it is not valid, you will get an error saying- "Command not found"
So remove spaces:
name="Stack Overflow"
and it will work fine.

Batch use "=" as a string when passing argument

funtion.bat echo variables
set "Var1=%1"
set "Var2=%2"
set "Var3=%3"
echo %Var1% %Var2% %Var3%
I use a batch that calls this function by passing 3 arguments
call function.bat blabla= argument2 TEST.txt
As you see my first argument has an equal sign in it. But I want to use it as a string and not as an operator.
When I run the batch this is the result that I get:
blabla
argument2
TEST.txt
This is the result that I want:
blabla=
argument2
TEST.txt
Does anyone have an idea of how to get "blabla="?
From cmd /? in cmd:
The special characters that require quotes are:
<space>
&()[]{}^=;!'+,~ `
As you can see, you should quote almost everything that contains = because it is used as a separator. You should run your batch file with the command:
call function.bat "blabla=" "argument2" "TEST.txt"
in cmd and then remove the double quotes for each argument using the following code (the ~ modifier):
set "Var1=%~1"
set "Var2=%~2"
set "Var3=%~3"
echo %Var1% %Var2% %Var3%
and it should work. This way is recommended for best practice. Do it always.

execute external program in lua without userinput as arguments in lua

I want to execute an external program in lua. Usually this can be done with
os.execute("run '"..arg0.."' 'arg1' arg2")
The problem with this approach is if I want to pass user input as string to an external program, user input could be '; evil 'h4ck teh system' ' and the script from above would execute like this:
/bin/bash -c "run ''; evil 'h4ck teh system' '' 'arg1' arg2"
Another problem occurs when I have '$var' as argument and the shell replaces this with its environment variable. In my particular case I have something like [[program 'set title "$My Title$"']] – so nested strings – and program parses "$My Title$" (with escape sequences) differently than '$My Title$' (as it is). Because I want to set the title as it, the best way is to have arguments like this: 'My Title'. But now the command have to be:
os.execute([[run "set title '$My Title$'"]])
But now – as I said – $My will be replaced with an empty string, because the environment does not know any variable named $My and because, I never wanted it to be replaced.
So I am looking for the usual approach with
execv("run", {"set title '"..arg0.."'", arg1, arg2})
local safe_unquoted = "^[-~_/.%w%%+,:#^]*$"
local function q(text, expand) -- quoting under *nix shells
-- "expand"
-- false/nil: $var and `cmd` must NOT be expanded (use single quotes)
-- true: $var and `cmd` must be expanded (use double quotes)
if text == "" then
text = '""'
elseif not text:match(safe_unquoted) then
if expand then
text = '"'..text:gsub('["\\]', '\\%0')..'"'
else
local new_text = {}
for s in (text.."'"):gmatch"(.-)'" do
new_text[#new_text + 1] = s:match(safe_unquoted) or "'"..s.."'"
end
text = table.concat(new_text, "\\'")
end
end
return text
end
function execute_commands(...)
local all_commands = {}
for k, command in ipairs{...} do
for j = 1, #command do
if not command[j]:match"^[-~_%w/%.]+$" then
command[j] = q(command[j], command.expand)
end
end
all_commands[k] = table.concat(command, " ") -- space is arguments delimiter
end
all_commands = table.concat(all_commands, ";") -- semicolon is commands delimiter
return os.execute("/bin/bash -c "..q(all_commands))
end
Usage examples:
-- Usage example #1:
execute_commands(
{"your/program", "arg 1", "$arg2", "arg-3", "~/arg4.txt"},
{expand=true, "echo", "Your program finished with exit code $?"},
{"ls", "-l"}
)
-- The following command will be executed:
-- /bin/bash -c 'your/program '\''arg 1'\'' '\''$arg2'\'' arg-3 ~/arg4.txt;echo "Your program finished with exit code $?";ls -l'
$arg2 will NOT be expanded into value because of single quotes around it, as you required.
Unfortunately, "Your program finished with exit code $?" will NOT be expanded too (unless you explicitly set expand=true).
-- Usage example #2:
execute_commands{"run", "set title '$My Title$'", "arg1", "arg2"}
-- the generated command is not trivial, but it does exactly what you need :-)
-- /bin/bash -c 'run '\''set title '\''\'\'\''$My Title$'\''\'\'' arg1 arg2'

In CMD substitution, how to remove substring which starts from specific character to the end

In substitution or deletion of sub string from original string, usually uses this form in windows CMD.
set result=%original:<strings_to_be_removed>=<strngs_to_be_newly_substituted>%
So it works for many situation as follows ..
set "original=Questions that may already have your answer"
set "you=%original:that=you%"
set you
you=Questions you may already have your answer
set challenge=%original:Questions=Challenges%
set challenge
challenge=Challenges that may already have your answer
set answer=%original:*your=%
set answer
answer= answer
But I don't know how to substitute or remove sub-string which starts from specific character(or word) to the end of the original string.
For example, suppose I would like to remove sub-string which starts from "that" to the end of the original string. Then I use command as follows and expect result string to be "Questions "
set result=%original:that*=%
But, result string has no difference from original string. No effect occures. Substitution intention fails..
set result
result=Questions that may already have your answer
I used escape character '^', '\' for this case, but no effect..
How to fix this to substitute or remove substring like this type?
How can you substitute or remove substring which starts from specific character(or word) to the end of the original string? Thank you:-)
you can trick the command line parser to do that:
set "original=Questions that may already have your answer"
set result=%original: may =&REM %
set result
sadly, set "result=%original:may=&REM %" doesn't work, so the string should be free from poison characters.
How it works:
replace the word with &REM, which makes your string:
Questions that & REM already have your answer
and the command:
set result=Questions that & REM already have your answer
& is used as a delimiter for commands (try echo hello&echo world, which executes both echo commands). So what's really executed, is two commands:
set result=Questions that
and
REM already have your answer
It also doesn't work with delayed expansion. You can use a subfunction for it instead:
setlocal enabledelayedexpansion
if 1==1 (
set "original=Questions that may already have your answer"
call :substring "!original!"
set result
)
goto :eof
:substring
set org=%~1
set result=%org: may =&REM %
goto :eof

Assigning one variable to another in Bash?

I have a doubt. When i declare a value and assign to some variable, I don't know how to reassign the same value to another variable. See the code snippet below.
#/bin/sh
#declare ARG1 to a
a=ARG1
#declaring $a to ARG2
ARG2=$`$a`
echo "ARG 2 = $ARG2"
It should display my output as
ARG 2 = ARG1
...but instead the actual output is:
line 5: ARG1: command not found
ARG 2 = $
To assign the value associated with the variable dest to the variable source, you need simply run dest=$source.
For example, to assign the value associated with the variable arg2 to the variable a:
a=ARG1
arg2=$a
echo "ARG 2 = $arg2"
The use of lower-case variable names for local shell variables is by convention, not necessity -- but this has the advantage of avoiding conflicts with environment variables and builtins, both of which use all-uppercase names by convention.
You may also want to alias rather than copy the variable. For example, if you need mutation. Or if you want to run a function multiple times on different variables. Here's how it works
Example:
C=cat
declare -n VAR=C
VAR+=" says Hi"
echo "$C" # prints "cat says Hi"
Example with arrays/dictionaries:
A=(a a a)
declare -n VAR=A # "-n" stands for "name", e.g. a new name for the same variable
VAR+=(b)
echo "${A[#]}" # prints "a a a b"
That is, VAR becomes effectively the same as the original variable. Instead of copying, you're adding an alias. Here's an example with functions:
function myFunc() {
local -n VAR="$1"
VAR="Hello from $2"
echo "I've set variable '$1' to value '$VAR'"
}
myFunc Inbox Bob # I've set variable 'Inbox' to value 'Hello from Bob'
myFunc Luke Leia # I've set variable 'Luke' to value 'Hello from Leia'
echo "$Luke" # Hello from Leia
Whether you should use these approaches is a question. Generally, immutable code is easier to read and to reason about (in almost any programming language). However, sometimes you really need to get stuff done in a certain way. Hope this answer helps you then.

Resources