how to write multiple exec arguments in ant - bash

I want to write an ant script that will
start up a new terminal
Change user
and run the Appium session
This is what i have so far but it doesn't do anything
<exec executable="/bin/bash" os="${os.unix}" spawn="true">
<arg value="-c" />
<arg value="gnome-terminal su appium" />
<arg value="appium &" />
</exec>

And what is the value of the property ${os.unix}? If you're going to use the os parameter, you usually give it a string constant and not a property value.
<exec executable="/bin/bash" os="unix" spawn="true">
<arg value="-c" />
<arg value="gnome-terminal su appium" />
<arg value="appium &" />
</exec>
This way, you could have an <exec> task for all Unix style operating systems, and another <exec> task for all the other ones (Windows).
Also understand the difference between <arg value="..."/> and <arg line="..."/>. I don't know the exact command structure for gnome-terminal, but when you pass something as a value, you're passing it as a single parameter -- even if it has spaces in it. For example:
<exec executable="foo">
<arg value="-f foo -b bar"/>
</exec>
Will execute as if I typed this in the command line:
$ foo "-f foo -b bar" # This is one command with one parameter. Note the quotation marks!
If I do this:
<exec executable="foo">
<arg line="-f foo -b bar"/>
</exec>
Will execute as if I typed this in the command line:
$ foo -f foo -b bar # This is one command with four parameters
This is equivalent to the above Ant task:
<exec executable="foo">
<arg value="-f"/>
<arg value="foo"/>
<arg value="-b"/>
<arg value="bar"/>
</exec>
Currently, you're attempting to execute:
$ /bin/bash -c "gnome-terminal su appium" "appium &"
If this is what you want, fine. By the way, you could skip the whole /bin/bash stuff on Unix:
<exec executable="gnome-terminal" os="unix" spawn="true">
<arg value="su appium"/>
<arg value="appium &"/>
</exec>

Try this:
<exec executable="/bin/bash" spawn="true" >
<arg value="-c" />
<arg value="x-terminal-emulator -e 'sudo -u appium appium'" />
</exec>
os="${os.unix}" seems incorrect, I've removed it completely.
-c and bash command needs to be in separate arg element.
su will start new shell. Use sudo with argument instead.
command passed to gnome-terminal needs to be quoted.
x-terminal-emulator should be more portable than gnome-terminal.
Actually, using bash doesn't seem to be necessary at all. Try:
<exec executable="x-terminal-emulator" spawn="true" >
<arg value="-e" />
<arg value="sudo -u appium appium" />
</exec>

Related

Sybase query:save an output to a file

I´ve created the following script:
#!/bin/bash
isql -U databasename_dba -P password -b <<EOF!
select quantity, date from name_table where numer_id="1234"
go
quit
EOF!
Running the script I got the desirable output, see:
user#system$ ./EXECUTE_DAILY_4:
But now, how can I save this result that I see in my terminal window in a file? (.csv for example)
I adapted the following to my Sybase query:
#!/bin/bash
cat > test.sql <<EOF!
isql -U databasename_dba -P password -b
select quantity, date from name_table where numer_id="1234"
go
quit
EOF!
isql test.sql >result.csv
Without success, the above is not working.
Thanks in advance
A couple options:
isql -U databasename_dba -P password -b <<-EOF > result.csv 2>&1
select quantity, date from name_table where numer_id="1234"
go
EOF
The '> result.csv 2>&1' says to write stdout to the file 'result.csv'; also redirect stderr (fd=2) to stdout (fd=1) (ie, also write stderr to the file 'result.csv'; from here you can do what you want with 'result.csv' (eg, check for errors, parse/process the file as needed, etc).
NOTE: The 'quit' is superfluous as the isql session will automatically exit/quit when it has nothing else to do.
If you want to place the query in a *.sql file:
echo "select quantity, date from name_table where numer_id='1234'" > test.sql
echo "go" >> test.sql
From here you have a couple options for submitting to isql:
isql -U databasename_dba -P password -b -i test.sql -o result.csv
or
isql -U databasename_dba -P password -b -i test.sql > result.csv 2>&1
The '-i test.sql' tells isql to take it's input from the file 'test.sql'; the first example uses '-o result.csv' to diredct stdout to 'result.csv' while the second example directs stdout/stderr to 'result.csv'.
You could effectively do the same thing but within the script itself. Something like:
#!/bin/bash
command=$(
isql -U databasename_dba -P password -b <<EOF!
select quantity, date from name_table where numer_id="1234"
go
EOF!
)
echo "$command" >> FILE.csv
I solved it using this (a basic solution):
./EXECUTE_DAILY_4> FILE.csv
I'm open to seeing more suggestions,
thanks.

Script runs in Windows but fails in Jenkins

I have a bash script :
C:/Jenkins/workspace/xmlstarlet-1.6.1-win32/xmlstarlet-1.6.1/xml.exe ed -L -s "/Package/types[name='$TYPENAME']" -t elem -n members -v "$ENTITY" $SCRIPTFILE
C:/Jenkins/workspace/xmlstarlet-1.6.1-win32/xmlstarlet-1.6.1/xml.exe ed -L -s "/Package" -t elem -n types -v "" $SCRIPTFILE
which inserts node for a particular type in an xml, this runs fine in local, but when trying to run via any script it fails:
Ant script:
<exec executable="C:\Program Files\Git\bin\bash.exe" osfamily="windows">
<arg value="generate_package.sh" />
<arg value="C:/Jenkins/workspace/TrailheadBranchDemo/final.txt" />
<arg value="package" />
<arg value="C:/Jenkins/workspace/deploy/src" />
</exec>
Error message :
Invalid expression: C:/Program Files/Git/Package
It is not able to pick the /Package or /Package/types[not(*)] expression.
The issue is :
on windows the bash script works like this:
$ C:/Jenkins/workspace/xmlstarlet/xmlstarlet/xml.exe ed -L -i "Package" -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" C:/Jenkins/workspace/deploy/src/package.xml
instead of this :
$ C:/Jenkins/workspace/xmlstarlet/xmlstarlet/xml.exe ed -L -i '/Package' -t attr -n xmlns -v "http://soap.sforce.com/2006/04/metadata" C:/Jenkins/workspace/deploy/src/package.xml
the /Package is not recognized well in windows.

Gnome Terminal: How to open up multiple tabs, execute commands and return to zsh/bash

I'm trying to automate my morning start up process. Often I'll start multiple running scripts that I will exit (using ctrl+c) and restart manually as needed.
So I'm looking to create a bash script that
Starts gnome-terminal
Opens some tabs and executes a number of
commands
Returns back to zsh upon exit or completion of the script so I can
manually enter more commands
Currently I have,
#!/bin/bash
gnome-terminal \
--tab -t "Server" -e "bash -ic \"cd ~/Dev/server; npm start; exec zsh\"" \
--tab -t "Framework" -e "bash -ic \"cd ~/Dev/framework; npm start; exec zsh\"" \
--tab -t "Client" -e "bash -ic \"cd ~/Dev/client; npm start; exec zsh\"" \
--tab -t "Admin" -e "bash -ic \"cd ~/Dev/admin;npm start; exec zsh\""
The problem with this solution is that tabs may or may not jump back into zsh. Sometimes 3 tabs will, sometimes one. Ideally I'd like all 4 to go back into zsh.
If anyone could help me around this, I'd be grateful.
You can take gnome-terminal out of the equation and it still won't work:
bash -ic "npm start; bash"
Press Control-C, and you won't get a shell. For bash, we can get around this problem using the --rcfile option:
bash --rcfile <(echo "npm start")
This way we don't read the real .bashrc, so since we probably want to read that, let's modify this a bit:
bash --rcfile <(echo ". ~/.bashrc; npm start")
For zsh on the other hand it doesn't work with the equivalent(?) --rcs option, but it does when started with the ZDOTDIR variable. So we create a .zshrc for each of ~/Dev/server, ~/Dev/framework, ~/Dev/client, ~/Dev/admin with the following content:
First:
. ~/.zshrc
cd ~/Dev/server
npm start
Second:
. ~/.zshrc
cd `~/Dev/framework`
npm start
etc.
Your gnome-terminal command will look like this:
gnome-terminal \
--tab "Server" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/first/.zshrc'" \
--tab "Framework" -e "sh -c 'ZDOTDIR=/path/to/directory/containing/second/.zshrc'"
# etc

Target directory with space in PuTTY pscp via NAnt

This target is OK (D:\Temp\sgr.tar.gz):
<target name="myTarget" description="Download application delivery file">
<exec program="pscp.exe">
<arg line="-batch -v -l ${ftp.user} -pw ${ftp.password} ${ftp.host}:${remote.dir}/${remote.file} D:\Temp\sgr.tar.gz"/>
</exec>
</target>
This target (with a space in target directory (D:\tmp 2\sgr.tar.gz)) is KO:
<target name="myTarget" description="Download application delivery file">
<exec program="pscp.exe">
<arg line="-batch -v -l ${ftp.user} -pw ${ftp.password} ${ftp.host}:${remote.dir}/${remote.file} D:\Temp\tmp 2.tar.gz"/>
</exec>
</target>
I have this error:
[exec] More than one remote source not supported
I try with but is KO also.
Wrap the path to double-quotes (&quot):
<arg line="-batch -v -l ${ftp.user} -pw ${ftp.password} ${ftp.host}:${remote.dir}/${remote.file} "D:\Temp\tmp 2.tar.gz""/>
See How to escape double quotes in XML attributes values?

Keep the gnome terminal open after the program is finished

This script opens a gnome terminal and 4 tabs in it , but once the program finishes the tabs gets closed so i can't see the output. It doesn't happen when i run each program manually. How can keep the tab open , even when the program is finished .
gnome-terminal --tab -e "optirun yarpserver" \
--tab -e "sh -c 'sleep 20 ; optirun iCub_SIM'" \
--tab -e "sh -c 'sleep 86 ; optirun simCartesianControl'" \
--tab -e "sh -c 'sleep 116 ; optirun iKinCartesianSolver --context simCartesianControl/conf --part left_arm'" \
Not sure how to do it on the command line (man gnome-terminal doesn't seem to indicate a specific option for that, but you can start a gnome-terminal, set specific options (one of which would be "When command exits: Hold the terminal open"), and save your settings as a specific profile. There is a command-line option for selecting a specific profile to use, so that should accomplish what you want.

Resources