convert backslashes to forward in batch files [duplicate] - bash

This question already has an answer here:
Windows shell string operations (changing backslash to slash)
(1 answer)
Closed 9 years ago.
whats the easiest way of converting all backslashes to forward in a path in a batch file, since I need to use bash for execution.

SET "string=D:\path\to\folder"
ECHO %string:\=/%
Basically, you need first to store the string value into an environment variable, then use the following template:
%variable:str1=str2%
to replace every occurrence of str1 in variable with str2.
You can always remind yourself about this pattern by invoking SET /? from the command prompt.

echo 'C:\Program Files\Program' | sed -e 's/\\/\//g'

Related

Bash - echo changes order of output, replacing strings [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
How to convert Windows end of line in Unix end of line (CR/LF to LF)
(8 answers)
Closed 29 days ago.
Trying to generate Wireguard key. And stuck with such behavior of echo command.
As you can see FINISH goes to the beginning and I can't understand how to avoid this.
#echo START$(wg genkey)FINISH
FINISH1111111111111111111111111111111111111111111
But in fact, the purpose of the command is to enclose the key in quotes.
With START/FINISH it’s clearer what happens.
#echo \"$(wg genkey)\"
"1111111111111111111111111111111111111111111
As a goal i need something like this, with existing quotes as part of an output.
"1111111111111111111111111111111111111111111"

What is the difference between % % and ! ! in a batch file? [duplicate]

This question already has answers here:
Difference between %variable% and !variable! in batch file
(2 answers)
Windows batch file syntax using exclamation mark
(3 answers)
Example of delayed expansion in batch file
(5 answers)
Closed 1 year ago.
What is the difference between these 2 pieces of code in a batch file?
set str=!str:%%20= !
and
set str=%str:%%20= %
Finally, after hours I could find the explanation.
According to this page
How-to: Escape Characters, Delimiters and Quotes at the Windows command line in the paragraph "Escaping Percents" :
Escaping Exclamation marks
When the shell is running in EnableDelayedExpansion mode the ! character is used to denote a variable and so must be escaped (twice) if you wish to treat it as a regular character:
^^!

Replacing substring with other text in variable inside a bash script [duplicate]

This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 5 years ago.
I've been trying to write a bash script. A part of it is supposed to replace a part of a string with nothing.
Here's what I'm trying to do
$dbname=$1
$dbNameActual="${$dbname/.sql/}"
date
echo $dbNameActual
I tried a number of suggestions from stack. But got nowhere. I tried adding sed, but that didn't seem to work.
The idea is that I have a script, and it takes in a db import file name, say db250317.sql and outputs db250317 .
I'm running Ubuntu 16.04 LTS.
You don't put $ twice in the expression, and you don't put $ before the variable you're assigning to (this isn't PHP or Perl). It should be:
dbNameActual="${dbname/.sql/}"
Also, if the thing you're trying to delete is always at the end, you can use % to remove it:
dbNameActual="${dbname%.sql}"
Also remember to quote the variable when you use it later, in case the filename contains spaces. You should almost always quote variables, unless you have a specific reason not to.

Replace with sed appears to do nothing [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 8 years ago.
I've this code in my script:
no_forward="#net.ipv4.ip_forward=1"
forward="net.ipv4.ip_forward=1"
sed -i 's/$no_forward/$forward/' /etc/sysctl.conf
Based on the man page, -i suffix is not neccesary, but this this use I'll only have the modified file instead both of them (the file modified and the "backup", the previous one).
I need to use that vars, because I needing them after that command so It's useful to have them like that. I guess I should be wrong with the pattern string, but right now I can't find why. Or maybe the problem are the var's strings or their symbols?
Could You help me? I accept other solutions non-sed based if they use bash and don't need special commands, since I'll need to use the script in another computer without installing anymore.
Thanks for reading
If you want to be able to use vars in sed, use double quotes, so :
sed -i "s/$no_forward/$forward/" /etc/sysctl.conf

Bash variable assignment and command not found [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 7 years ago.
I have a shell script that will let me access global variables inside the script, but when I try to create my own, it responds with: command not found.
#!/bin/bash
J = 4
FACE_NAME = "eig$J.face"
USER_DB_NAME = "base$J.user"
When I run the above script I get:
./test1.sh line 2: J: command not found
./test1.sh line 3: FACE_NAME: command not found
./test1.sh line 4: USER_DB_NAME: command not found
Any ideas?? I'm using Cygwin under Windows XP.
Try this (notice I have removed the spaces from either side of the =):
#!/bin/bash
J="4"
FACE_NAME="eig$J.face"
USER_DB_NAME="base$J.user"
Bash doesn't like spaces when you declare variables - also it is best to make every value quoted (but this isn't as essential).
It's a good idea to use braces to separate the variable name when you are embedding a variable in other text:
#!/bin/bash
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"
The dot does the job here for you but if there was some other character there, it might be interpreted as part of the variable name.
dont' leave spaces between "="
J=4
FACE_NAME="eig${J}.face"
USER_DB_NAME="base${J}.user"

Resources