How do I use a variable in double quotation on a batch script [closed] - windows

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
I am writing a batch script on windows to automate downloading files with using WinSCP. I have the following code snippet and it works perfectly; however, depending on the user the user name is different so I want user to be a variable that users can specify.
#echo off
set message=Starting something cool
echo %message%
:: This is comment ?
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Users\this_user\Desktop\temp_log\WinSCP.log" ^
/ini=nul ^
/command ^
"open sftp://test:crl#192.168.113.133/ -hostkey=""ssh-ed25519 255 H23HQ7fVOAun2yaVJ6tZwOWyIQ5nkmhiWz+gfxSnwRQ""" ^
"get /var/log/kern.log C:\Users\this_user\Desktop\temp_log\sys\" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
So I want "this_user" to be a variable
I tried the following:
THIS_USER="this_user"
C:\Users\this_user\Desktop\$THIS_USER
but no luck.

Related

How to remove double quotes without escape (^) in cmd/ batchfile? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
Note: as the OP is a new user, I'm editing the question with reproducible details so that it doesn't get closed. It's a good question, but was originally confusing.
The Windows username is: jhon^smith
ECHO %username%
output : jhonsmith
My expectation:
jhon^smith
If I use:
echo "%username%"
then the output is:
"jhon^smith"
How can I remove the double quotes but keep the caret, which is actually part of the username?
This should work:
for /f %a in ("%username%") do echo %a
output:
jhon^smith
(btw: It should be spelt john^smith but never mind.)
Explanation:
In the command echo %username%, cmd.exe replaces %username% with its actual content, so the command becomes echo jhon^smith. This is then parsed, the the ^ is processed as the escape character, so it replaces ^s with s.
In the command for /f %a in ("%username%") do echo %a, the quoted string is treated as a string without the quotes, and no parsing of ^ is performed. Type for /? for more details.

Bash string literal comparison to variable failing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have the following simple bash code to test string comparison:
#!/bin/sh
BRANCH="master"
echo $ref
if [[ "$ref" = "refs/heads/$BRANCH" ]]
then
echo "Matches"
else
echo "Do not match"
fi
When I ran the code using export ref=/refs/heads/master && . sample I get the following result:
/refs/heads/master
Do not match
What may be causing the problem?
What is causing the problem is the missing slash in your test: /refs/heads/master is not equal to refs/heads/master!

Batch remove parentheses and square brackets from file name [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I would like to know how to remove parentheses and square brackets and what's inside of them in a list of files' names, using a .bat file
For example:
[Tag]File (1).mkv
to
File.mkv
The solution to your issue can be found in syntax-replacement. The first thing you want to do is have the batch file turn your file name into a variable. From there, you can use syntax-replacement with the set= to replace/remove parts of the variable.
The script bellow will remove [square brackets] and the contents inside them, and (parentheses) and the contents inside them.
Please keep in mind that this script bellow in an example to demonstrate syntax-replacement. It will not check/test (if) somethings in-front or behind. It expects you to have it formatted as [Tag]Name(#).ex - Change it to your needs.
#echo off
#setlocal
::Input your file location bellow.
call :edit "C:\Users\%username%\Desktop\[TAG]Hello(1).jpg"
goto :EOF
:edit
::Define file as variable
set file=%~1
set start=%~1
::Define file extension as variable
set ext=%file:*.=%
::Remove right square bracket and everything before it
set square=%file:*]=%
::Remove everything after left parentheses
set efile=%square%
set endbit=%efile:*(=%
call set result=%%efile:%endbit%=%%
::Remove left parentheses
set final=%result:(=%
::Add file extension and rename
ren %start% "%final%.%ext%"
exit

Unable to find the reason for this: bash missing' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
why am I getting "bash missing'" for this:
function get_xserver ()
{
case $TERM in
xterm )
XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
;;
aterm | rxvt)
# Find some code that works here. ...
;;
esac
}
This is the exact error:
bash: [: missing `]'
The error is not in the code you posted. The error message:
-bash: [: missing `]'
Means exactly what it says - there is a missing ] character, namely in a [ test ] statement.
Try it:
$ [ 1 -eq 2
-bash: [: missing `]'
You need to identify where the error actually is, and add the missing closing bracket.

Append to variable within shell script loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to loop over all environment variables in a shell script, and create an HTML query string from ones which match a pattern. Unfortunately, I can't seem to assign to variables in the loop. I've got this:
#!/bin/sh
IFS=$'\n'
TAGS=""
for item in $(printenv)
do
if [[ $item == FOO_TAG_* ]]
then
TAGS = "${TAGS}&${item}"
fi
done
But this gives me
/etc/script.sh: line 9: TAGS: command not found
/etc/script.sh: line 9: TAGS: command not found
How do I fix this?
In the assignment, remove space between variable name and =
TAGS="${TAGS}${item}"

Resources