how to run unix command with tcl/expect - tcl

I'm new to tcl. I'm trying to run some commands with tcl script.
I searched online and came to know that we can run unix commands with tcl using exec
I executed the following;
perl -i -p -e 's/hello linux./hello fedora. /g;' sample1.txt
from the commandline and it worked. it replaced all the occurences of hello linux with hello fedora.
I tried executing the same command in my tcl script.
set result [exec perl -i -p -e 's/hello linux./hello fedora. /g;' sample.txt]
I got the below error :
child process exited abnormally
I also tried using sed command. I got the same error. I guess there is something wrong with the syntax. I searched online but i couldn't figure it out on my own.

' is not a grouping character in tcl. The equivalent grouping in tcl is {}. Therefore the correct statement is:
exec perl -i -p -e {s/hello linux./hello fedora. /g;} sample.txt
Or even:
exec perl -i -p -e "s/hello linux./hello fedora. /g;" sample.txt

Related

Opening gnome-terminal and running script in tcl

When I try to execute below command it is showing Error like "extra characters after close-quote" but I gave it properly & when i try to it in unix command line terminal is opening properly.
exec gnome-terminal -e 'sh -c "bsub -Ip -n 1 -M <Memory> -q <queue_name> make"'
Can any one help me to resolve this issue or is there any way to do the same thing ??
Edited -> changed " from before sh to before bsub
Tcl's quoting is not the shell's quoting. Tcl uses {…} like the shell uses single quotes, except that braces nest nicely. Nesting single quotes is a recipe for shell headaches.
exec gnome-terminal -e {sh -c "bsub -Ip -n 1 -M <Memory> -q <queue_name> make"}
However, in this case I'd instead be tempted to go with this:
set memory "<Memory>"
set queue "<queue_name>"
set command "make"
set bsubcmd "bsub -Ip -n 1 -M $memory -q $queue $command"
# It's much more convenient to build this command like this here.
# Otherwise you're doing lots of backslashes and so on and it's horrible and very easy to make bugs
exec gnome-terminal -e [format {sh -c "%s"} $bsubcmd]
The only really messy thing is that command and bsubcmd have to be built using shell syntax if you're passing spaces around. “Fortunately” you're dealing with make anyway, so you probably really want to avoid having spaces in names passed there.

Is it possible to pass a determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so how?

So I'm writing an install script and because I haven't been able to find a solid MySQL replacement on armfh (the db must be MySQL compatible), I'm using a community on that works, however it does not initiate the db as it should. it requires me to pass the following argument.
mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql
From inside the docker. Problem is I want this to flow naturally as a smooth install script. I've tried using the following command to pass the document and get a password prompt:
docker exec -it db bash -c "mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql"
If also tried:
docker exec -it db bash -c "mysql -h'db' -u'root' -p'$MYSQL_ROOT_PASSWORD' '$MYSQL_DATABASE' < /docker-entrypoint-initdb.d/1_db.sql
FwIW: I used the MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1) to define the password. And if I manually enter the docker send command 1 (in quotations) the db initiates.
So to summarize my question: Is it possible to pass a command like the above to activate the 1_db.sql file from outside docker?
Any help would be amazing! Thanks in advance!
Is it possible to pass a command like the above to activate the
1_db.sql file from outside docker?
you can try something like
cat 1_db.sql | docker exec -i test bash -c 'mysql -uroot -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE'
also, remember when you try exec bash -c "mysql -uroot -p$MYSQL_ROOT_PASSWORD"it will for MYSQL_ROOT_PASSWORD in the host, not inside container, use single quotes.
determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so
how?
docker exec -i test bash -c 'echo mysql docker password is $MYSQL_ROOT_PASSWORD'

Mysql cli not returning data in bash script run by crontab

I have a bash script that is executed via a cron job
#!/bin/bash
# abort on errors
set -e
ABS_DIR=/path/
# extract the creds for the mysql db
DB_USER="USERNAME"
DB_PASS="PASSWORD"
function extract_data() {
file=$2
sql_query=`cat $ABS_DIR/$1`
data=`mysql -u $DB_USER --password="$DB_PASS" -D "database" -e "$sql_query" | tail -n +2`
echo -e "Data:"
echo -e "$data"
}
extract_data "sql_query.sql" "log.csv"
When running it manually with bash extract.sh the mysql cmd fetches the data correctly and I see the echo -e "$data" on the console.
When running the script via a cron job
* 12 * * * /.../extract.sh > /.../cron_log.txt
then I get an empty line saved to the cron_log.txt file!?
This is a common problem; a script behaves differently when run from user shell and when run from crontab. The cause is typically due to differences in the environment variables in the user shell, and in the crontab shell; by default, they are not the same.
To begin debugging this issue, you could direct stderr as well as stdout from crontab, hopefully to capture an error message:
extract.sh &> /.../cron_log.txt
(notice the &)
Also: you have three dots (/.../) -- that is likely a typo, could also be the cause.

Hyphen and/or period in environment variable name is causing issue

This is working.
oc new-app --docker-image=docker.mycompany.com/myusername/my-imagestuff:latest -e SPRING_DATASOURCE_URL="jdbc:sqlserver://blahblahblah;” -e SPRING_DATASOURCE_USERNAME=“myUserName1” -e SPRING_DATASOURCE_PASSWORD=“MyP#ssword” -e
so I went back and added the datasource-classname
oc new-app --docker-image=docker.mycompany.com/myusername/my-imagestuff:latest -e SPRING_DATASOURCE_URL="jdbc:sqlserver://blahblahblah;” -e SPRING_DATASOURCE_USERNAME=“myUserName1” -e SPRING_DATASOURCE_PASSWORD=“MyP#ssword” -e SPRING_DATASOURCE_DRIVER-CLASS-NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"
and now my deployments are failing with this error:
error: invalid parameter assignment in
"SPRING_DATASOURCE_DRIVER-CLASS-NAME=com.microsoft.sqlserver.jdbc.SQLServerDriver"
What is the magic sauce of hyphen / hyphens and/or periods / dots in the environment variable and value
?
Thanks!
You're unable to use environment variables that contain hyphens or periods because those characters are not valid in shell variable names:
➜ tmp.0ngsgXro foo.bar=1
zsh: command not found: foo.bar=1
➜ tmp.0ngsgXro foo-bar=1
zsh: command not found: foo-bar=1
➜ tmp.0ngsgXro foo_bar=1
➜ tmp.0ngsgXro echo $foo_bar
1
You're trying to create an environment variable in your container that violates the rules of the underlying shell.
So out of desperation, I changed the hyphens to underscores.
Note the last argument of SPRING_DATASOURCE_DRIVER_CLASS_NAME
oc new-app --docker-image=docker.mycompany.com/myusername/my-imagestuff:latest -e SPRING_DATASOURCE_URL="jdbc:sqlserver://blahblahblah;” -e SPRING_DATASOURCE_USERNAME=“myUserName1” -e SPRING_DATASOURCE_PASSWORD=“MyP#ssword” -e SPRING_DATASOURCE_DRIVER_CLASS_NAME="com.microsoft.sqlserver.jdbc.SQLServerDriver"
And my app is working. So I am writing this answer.
I do not understand this voodoo.
If someone can explain it, I'd be grateful.

How to Pass Parameters from QSub to Bash Script?

I'm having an issue passing variables to a Bash script using QSub.
Assume I have a Bash script named example. The format of example is the following:
#!/bin/bash
# (assume other variables have been set)
echo $1 $2 $3 $4
So, executing "bash example.sh this is a test" on Terminal (I am using Ubuntu 12.04.3 LTS, if that helps) produces the output "this is a test".
However, when I enter "qsub -v this,is,a,test example.sh", I get no output. I checked the output file that QSub produces, but the line "this is a test" is nowhere to be found.
Any help would be appreciated.
Thank you.
Using PBSPro or SGE, arguments can simply be placed after the script name as may seem intuitive.
qsub example.sh hello world
In Torque, command line arguments can be submitted using the -F option. Your example.sh will look something like this:
#!/bin/bash
echo "$1 $2"
and your command like so:
qsub -F "hello world" example.sh
Alternatively, environment variables can be set using -v with a comma-separated list of variables.
#!/bin/bash
echo "$FOO $BAR"
and your command like so:
qsub -v FOO="hello",BAR="world" example.sh
(This may be better phrased as a comment on #William Hay's answer, but I don't have the reputation to do so.)
Not sure which batch scheduler you are using but on PBSPro or SGE then submitting with qsub example.sh this is a test should do what you want.
The Torque batch scheduler doesn't (AFAIK) allow passing command line arguments to the script this way. You would need to create a script looking something like this.
#!/bin/bash
echo $FOO
Then submit it with a command like:
qsub -v FOO="This is a test" example.sh