I'm adding the following line to a Batch file using:
ECHO enter.php?login=%s&pass=%s >>textfile.txt
the Output is coming as
enter.php?login=s
This is my first Batch file.
Double the % - Normally ^ escapes awkward characters like > so to have them reproduced literally, you'd use ^>. % is different - you need %% for each % you want to "print"
Related
I am trying to upload a file to an FTP server using a batch command file. My code uses WinSCP and looks like this:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/log="C:\Program Files (x86)\WinSCP\WinSCP.log" /ini=nul ^
/command ^
"open ftp://user:pass#ftp.website.org/" ^
"lcd %cd%" ^
"cd /incoming/data" ^
"put %~dp0file.txt" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
The log shows this:
> 2020-10-14 15:16:39.044 Script: put C:\Users\me\sharepoint - library folder\FTP\file.txt
. 2020-10-14 15:16:39.047 Copying 4 files/directories to remote directory "/incoming/data" - total size: 0
. 2020-10-14 15:16:39.047 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: folder\FTP\file.txt
. 2020-10-14 15:16:39.047 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2020-10-14 15:16:39.047 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
* 2020-10-14 15:16:39.048 (EOSError) System Error. Code: 2.
* 2020-10-14 15:16:39.048 The system cannot find the file specified
I'm expecting put %~dp0file.txt to give the full path to the file I want to upload and it looks like it gets it right from the log, but I don't know why it's trying to send 4 files or why it fails to find the specified file. I should note the path (folder/ftp) is actually much longer, the full path is 102 characters. The path contains spaces and one dash.
Your path contains spaces, so it has to be wrapped to double quotes:
"put ""%~dp0file.txt""" ^
The same goes for lcd. But as it makes no sense to use lcd, if you use an absolute path in the put command anyway, you can just remove the lcd altogether.
So, I have an app. And it's not written by me. And it's a command line app. It outputs some strings which I am able to write to a file, like that:
anApp -input myFile.txt > myFileOutput.txt
The problem is that the output is way too large and the computer runs out of memory. Is it possible to do something like that:
anApp -input myFile.txt > i=0; for each 100000 lines; touch newFile%d $(i++); $cat 100000lines >> newFile%d $(i++); done
Because it is rather a clumsy pseudocode, I am also adding explanation:
For each 100000 lines (for instance)
Create a new file called: newFile# - where # is a number from 0 to n
Write those 100000 lines to a newly created file.
I think there may be also another option - to keep the output of the anApp in cash. However, the file's huge, it contains some results and if it will be lost... It's not something which I would like to happen.
One option would be to use split:
anApp -input myFile.txt | split -l 100000 - myFileOutput
This will generate files with names like myFileOutputaa, myFileOutputab, etc.
For more control over the names of the output files, you could use awk:
NR % 100000 == 1 { close(outfile); outfile = sprintf("myFileOutput%02d", i++) }
{ print > outfile }
You can save that script to a file and run it like:
anApp -input myFile.txt | awk -f script.awk
I need to create a dynamic variable name for some arrays and fill it with multi-line text.
What I actually have is this :
#!/bin/bash
IFS=$'\n'
# Set an array with 1 item
ARRAY=("Item1")
# Get a description for the last item from a simple text file
DESCRIPTION=("$(cat test1.txt)")
# Get the number of items in the array
ARRAY_ITEMS_COUNT=${#ARRAY[#]}
# Create a variable name containing the number of items in the array as identifier
# and fill it with the description
eval ARRAY_ITEM${ARRAY_ITEMS_COUNT}_DESCRIPTION="(\"${DESCRIPTION[#]}\")"
# Display some results
echo "ARRAY_ITEM1_DESCRIPTION[#] = \"${ARRAY_ITEM1_DESCRIPTION[#]}\""
echo "ARRAY_ITEM1_DESCRIPTION[0] = \"${ARRAY_ITEM1_DESCRIPTION[0]}\""
echo "ARRAY_ITEM1_DESCRIPTION[1] = \"${ARRAY_ITEM1_DESCRIPTION[1]}\""
echo
# Same as above with a different text file
ARRAY=("Item1" "Item2")
DESCRIPTION=("$(cat test2.txt)")
ARRAY_ITEMS_COUNT=${#ARRAY[#]}
# Get an error here due to the ' character used in the text file
eval ARRAY_ITEM${ARRAY_ITEMS_COUNT}_DESCRIPTION="(\"${DESCRIPTION[#]}\")"
echo "ARRAY_ITEM2_DESCRIPTION[#] = \"${ARRAY_ITEM2_DESCRIPTION[#]}\""
echo "ARRAY_ITEM2_DESCRIPTION[0] = \"${ARRAY_ITEM2_DESCRIPTION[0]}\""
echo "ARRAY_ITEM2_DESCRIPTION[1] = \"${ARRAY_ITEM2_DESCRIPTION[1]}\""
The files "test1.txt" and "test2.txt" are as follow :
test1.txt
Simple text file with multi-lines used as
a test without special characters inside.
test2.txt
Simple text file with multi-lines used as
a test with single ' and double " quotes.
Expected result :
ARRAY_ITEM1_DESCRIPTION[#] = "Simple text file with multi-lines used as
a test without special characters inside."
ARRAY_ITEM1_DESCRIPTION[0] = "Simple text file with multi-lines used as"
ARRAY_ITEM1_DESCRIPTION[1] = "a test without special characters inside."
ARRAY_ITEM2_DESCRIPTION[#] = "Simple text file with multi-lines used as
a test with single ' and double " quotes."
ARRAY_ITEM2_DESCRIPTION[0] = "Simple text file with multi-lines used as"
ARRAY_ITEM2_DESCRIPTION[1] = "a test with single ' and double " quotes."
Current result :
ARRAY_ITEM1_DESCRIPTION[#] = "Simple text file with multi-lines used as
a test without special characters inside."
ARRAY_ITEM1_DESCRIPTION[0] = "Simple text file with multi-lines used as
a test without special characters inside."
ARRAY_ITEM1_DESCRIPTION[1] = ""
./test.sh: eval: line 28: unexpected EOF while looking for matching `"'
./test.sh: eval: line 29: syntax error: unexpected end of file
ARRAY_ITEM2_DESCRIPTION[#] = ""
ARRAY_ITEM2_DESCRIPTION[0] = ""
ARRAY_ITEM2_DESCRIPTION[1] = ""
I tried a lot of things but it never gives me what is expected, so can someone help me solve the 2 issues I have there please :
How to get proper array containing each line of the text file on each index
How to make it work even with quotes characters in the texts
EDIT : Working solution (bash version > 4) is :
#!/bin/bash
# Set an array with 1 item
ARRAY=("Item1")
# Get the number of items in the array
ARRAY_ITEMS_COUNT=${#ARRAY[#]}
# Create a variable name containing the number of items in the array as identifier
# and fill it with the description
readarray -t "ARRAY_ITEM${ARRAY_ITEMS_COUNT}_DESCRIPTION" < test1.txt
# Display some results
echo "ARRAY_ITEM1_DESCRIPTION[#] = \"${ARRAY_ITEM1_DESCRIPTION[#]}\""
echo "ARRAY_ITEM1_DESCRIPTION[0] = \"${ARRAY_ITEM1_DESCRIPTION[0]}\""
echo "ARRAY_ITEM1_DESCRIPTION[1] = \"${ARRAY_ITEM1_DESCRIPTION[1]}\""
echo
# Same as above with a different text file
ARRAY=("Item1" "Item2")
ARRAY_ITEMS_COUNT=${#ARRAY[#]}
# Get an error here due to the ' character used in the text file
readarray -t ARRAY_ITEM${ARRAY_ITEMS_COUNT}_DESCRIPTION < test2.txt
echo "ARRAY_ITEM2_DESCRIPTION[#] = \"${ARRAY_ITEM2_DESCRIPTION[#]}\""
echo "ARRAY_ITEM2_DESCRIPTION[0] = \"${ARRAY_ITEM2_DESCRIPTION[0]}\""
echo "ARRAY_ITEM2_DESCRIPTION[1] = \"${ARRAY_ITEM2_DESCRIPTION[1]}\""
Thanks for your help, have a nice day.
Slander
When you call cat in an array assignment you shouldn't quote it if you want the file to be read line by line. Because if you do so the contents of the file will be handled as one string/one line. So it won't get read line by line. Just try:
DESCRIPTION=($(cat test1.txt))
Also if you are using Bash version 4 you could use bash builtin command readarray to generate an array:
readarray -t DESCRIPTION < "test1.txt"
For Bash version < 4 this could be an alternative to cat and readarray:
IFS=$'\n' read -d -r -a DESCRIPTION < "test1.txt"
If on the command line I execute:
c:\digitemp.exe -t0 -o%C -q > res1.txt
res1.txt contains correctly the numerical temperature in Celsius (say: 24.23456). But if the same command is executed inside a bat file (say: test.bat):
#ECHO OFF
ECHO Hola pootol!
ECHO.
c:\digitemp.exe -t0 -o%C -q > res1.txt
rem set pootol = < res1.txt
rem set pootol
ECHO Prem una tecla per sortir.
pause > null
res1.txt contains a wrong Celsius value that I suspect is related to the argument " -o%C ". As you can see I rem the variable assing cause pootol var is wrong assigned with the Celsius value before it is mentioned. What am I doing wrong?
The problem in your case is the % sign, as it's evaluated different in the cmd-line and in batch files.
In batch files you can escape it with doubling it.
So your code looks like
c:\digitemp.exe -t0 -o%%C -q > res1.txt
In batch files % is used to denote variables. So %C is interpreted inside the batch file as a variable and replaced with its value. Since it doesn't have a value it is replaced with an empty string.
Use the caret ^ character to escape the % so that the interpreter treats the % as a normal character.
c:\digitemp.exe -t0 -o^%C -q > res1.txt
I'm creating a Shell Script, and I have a file like this called expressions.txt:
"Ploink Poink"
"I Need Oil"
"Some Bytes are Missing!"
"Poink Poink"
"Piiiip Beeeep!!"
"Whoops! I'm out of memory!"
"1 + 1 = 3"
"Please fix my bugs!"
"Goeiedag!"
"Hallo!"
"Guten Tag!"
"Hyvää Päivää!"
"Добрый день"
"!สวัสดี"
"Bonjour!"
"!مرحبا"
"!שלום"
"Γειά!"
I have this code in robot.sh:
expressions=( $(cat expressions.txt) )
# Get random expression...
selectedexpression=${expressions[$RANDOM % ${#expressions[#]}]}
# Write to Shell
echo $selectedexpression
However, this splits the file's content by spaces, not by quotes, so the output could be something like "Ploink, Need or fix. But I want the complete sentences. Is there a way to do this? Thanks
Oh, the shell I use is #!/bin/sh.
This will work with bash, possibly with sh:
OLDIFS="$IFS"
IFS=$'\n'
expressions=()
while read line
do
expressions=("${expressions[#]}" "$line")
done < expressions.txt
IFS="$OLDIFS"
use (g)awk, or nawk on Solaris
awk 'BEGIN{srand() }
{
lines[++d]=$0
}END{
RANDOM = int(1 + rand() * d)
print lines[RANDOM]
}' expression.txt