Generate an HTML file with variables in shell (Automator) - html

Basically, I have this "workflow" that I find myself doing frequently and would love to automate:
create a folder with a new name in a specific folder (the path doesn't change)
create an index.html file in that folder
edit the index.html with 2 key variables (A web title and an https: link)
run a script
Here's how far I've gotten in Automator:
Ask for new folder name
Save as variable
Ask for web title name
Save as variable
Ask for link
Save as variable
Run shell script to cd to the right folder and "touch index.html"
Now I'm stuck. How would I edit the index.html while using the two other variables mentioned. Is there a way to edit or "replace" the file's contents while using Automator variable?
TIA!

Try adding the following to 'Run Shell Script' in the Automator workflow:
for var in $#
do
echo $var >> /path/to/index.html
done
and then setting "Pass input:" above the "Run Shell Script" module to: 'as arguments'
What this loop does is run the commands between do and done for every single variable you set in your Automator script. Alternatively, you can just replace for var in $# to for var, as an empty for will automatically collect the variables.
> and >> are bash shell operators. >> appends to a file or creates the file if it doesn't exist. The > overwrites the file if it exists or creates it if it doesn't exist. You may remove the touch command, unless you wish to create an empty file no matter if any variables are supplied.
If you need to differentiate between your variables, you don't even need a for loop, and can simply run:
echo $1 >> /path/to/index.html
echo $2 >> "/path to/index.html" # *or* /path\ to/index.html
# ^ if the directory of the file contains spaces
echo "The third supplied variable is: ${3}" >> /path/to/index.html
# ^ if you wish to add additional text to the variable
and so on, following the order in which you set your automator variables. Just make sure "Pass input:" is still set to 'as arguments'.

Related

Call a Powershell script function from CMD with parameters

Maybe i need a pair of fresh eyes on that:
i use the following code in a .bat file in order to call a Powershell script which accepts 2 parameters.And i want to pass the same parameters in a function which is the Powershell script.The problem is that i cannot seperate the 2 parameters inside the function.Here is the code inside the .bat file :
#ECHO OFf
Set OUTFILE="C:\users\XXX\desktop\newchangepass\log.txt"
echo user='%1' pass='%2' >> %OUTFILE%
echo %1 %2
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -File "C:\ps\CallMeWithParams.ps1" %1 %2
That's what i give to the command line:
c:\PS>CallMeWithParams.bat Alpha Beta
and here is the code inside the Powershell script:
param ([Parameter (Mandatory)]$param1, [Parameter(Mandatory)]$param2 )
function foo{
param ($fusername,$fpassword)
write-output $fusername
}
foo($param1,$param2)
The output which i receive obviously in CMD is:
c:\PS>CallMeWithParams.bat Alpha Beta
Alpha Beta
Alpha
Beta
i should get only the first parameter....
The code inside the PS script is there for test reasons.I want to be sure that i get the two parameters and that i can seperate them in order to use the second one in an update statement.
Any suggestion would be greatly appreciated.
PS1: Yes the CMD arguments will be enclosed in double quotes
PS2: atm there is no control on variable types etc. I wanted to keep it simple for debugging reasons.

Local variable in makefile is not expanded correctly

I have the following function in makefile:
define INSTALL_SCRIPT
SRC_DIR = $(ROOT)\src
cd $(SRC_DIR)
$(SRC_DIR)\stage.bat
endef
I also echo the steps, so here's the output of the above snippet:
$SRC_DIR = C:\project_root\src
'SRC_DIR' is not recognized as an internal or external command,
operable program or batch file.
$cd
C:\project_root
\stage.bat
'\stage.bat' is not recognized as an internal or external command,
operable program or batch file.
It seems that in assignment statement the value is expanded correctly but then $(SRC_DIR) gives an error. Then cd goes to one directory up (and not src), then when I need to execute the batch file, $(SRC_DIR)'s value seems to be empty.
Assuming you're trying to do this from a recipe context, you would need to do it as follows:
define INSTALL_SCRIPT
set SRC_DIR=$(ROOT)\\src & \
cd %SRC_DIR% & \
%SRC_DIR%\\stage.bat
endef
sometarget:
#$(INSTALL_SCRIPT)
You need the \ at the end of each line to concatinate them into a single recipe line (other wise the variable you set will fall out of context when the first recipe line's shell terminates). You seem to be using windows so I believe you need to use the %varname% syntax to refer to the variables. Notice that $(ROOT) is a makefile variable in this case, so it still uses the $ syntax. (Note that if you were in bash you would need to use $$ to refer to shell variables). You also need to double the \\ in directory names, as make will interpret the first slash as an escape, and then pass a single slash to cmd.
Note that my windows machine doesn't have make installed on it, so I couldn't test the above, so it's quite possible I missed something.

How to copy or move multiple files with same extension?

So I am trying to move a bunch of files with similar extensions from /home/ to /root/
Code I tried is
file copy /home/*.abc.xyz /root/
Also tried
set infile [glob -nocomplain /home/*.abc.xyz ]
if { [llength $infile] > 0 } {
file copy $infile /root/
}
No success.
Your two attempts fail for different reasons:
There is no wildcard expansion in arguments to file copy, or any Tcl command, for that matter: file copy /home/*.abc.xyz /root/. This will look for a single source with a literal * in its filename.
glob -nocomplain /home/*.abc.xyz is ok to collect the sources, but glob returns a list of sources. file copy requires each source to passed as a separate argument, not a single one. To expand a single collection value of source files into a multiple separate arguments, use the Tcl expansion operator {*}
Therefore:
set infiles [glob -nocomplain *.tcl]
if {[llength $infiles]} {
file copy {*}$infiles /tmp/tgt/
}
For a 1-line answer:
file copy {*}[glob /home/*.abc.xyz] /root/.
The file copy (and file rename) commands have two forms (hence the reference to the manual page in the comment). The first form copies a single file to a new target. The second form copies all the file name arguments to a new directory and this form of the command insists that the directory name be the last argument and you may have an arbitrary number of source file names preceding. Also, file copy does not do glob expansion on its arguments, so as you rightly surmised, you also need to use the glob command to obtain a list of the files to copy. The problem is that the glob command returns a list of file names and you passed that list as a single argument, i.e.
file copy $infile /root/
passes the list as a single argument and so the file copy command thinks it is dealing with the first form and attempts to find a file whose name matches that of the entire list. This file probably doesn't exist. Placing the error message in your question would have helped us to know for sure.
So what you want to do is take the list of files contained in the infile variable and expand it into separate argument words. Since this is a common situation, Tcl has some syntax to help (assuming you are not using some ancient version of Tcl). Try using the command:
file copy {*}$infile /root/
in place of your first attempt and see if that helps the situation.

How to zip multiple files through tcl script in Linux box?

I have set of code in tcl where I'm trying to achieve to zip the files but I'm getting below error
zip warning: name not matched: a_1.txt a_2.txt a_3.txt a_4.txt
On other hand I'm doing same thing from command prompt I'm able to execute successfully.
#!/usr/local/bin/tclsh
set outdir /usr/test/
set out_files abc.10X
array set g_config { ZIP /usr/bin/zip }
set files "a_1.txt a_2.txt a_3.txt a_4.txt"
foreach inp_file $files {
append zipfiles "$inp_file "
}
exec $g_config(ZIP) $outdir$out_files zipfiles
Tcl really cares about the boundaries between words, and doesn't split things up unless asked to. This is good as it means that things like filenames with spaces in don't confuse it, but in this case it causes you some problems.
To ask it to split the list up, precede the read of the word from the variable with {*}:
exec $g_config(ZIP) $outdir$out_files {*}$files
This is instead of this:
exec $g_config(ZIP) $outdir$out_files $files
# Won't work; uses "strange" filename
or this:
exec $g_config(ZIP) $outdir$out_files zipfiles
# Won't work; uses filename that is the literal "zipfiles"
# You have to use $ when you want to read from a variable and pass the value to a command.
Got a very old version of Tcl where {*} doesn't work? Upgrade to 8.5 or 8.6! Or at least use this:
eval {exec $g_config(ZIP) $outdir$out_files} $files
(You need the braces there in case you put a space in outdirā€¦)

Export blob from mysql to file in bash script

I'm trying to write a bash script that, among other things, extracts information from a mysql database. I tried the following to extract a file from entry 20:
mysql -se "select file_column_name from table where id=20;" >file.txt
That gave me a file.txt with the file name, not the file contents. How would I get the actual blob into file.txt?
Turn the value in file.txt into a variable and then use it as you need to? i.e.
blobFile=$(cat file.txt)
echo "----- contents of $blobFile ---------"
cat $blobFile
# copy the file somewhere else
scp $blobFile user#Remote /path/to/remote/loc/for/blobFile
# look for info in blobfile
grep specialInfo $blobFile
# etc ...
Is that what you want/need to do?
I hope this helps.