python sage attach from command line and run another command - command-line

I want to load a sage file and run a function from that file, all from bash.
The attach command is giving me trouble...
This works:
bash-prompt$ sage -c "print 2; print 3"
2
3
bash-prompt$
This also works:
bash-prompt$ sage -c "attach somefile.sage"
some print just to confirm the file is loaded
bash-prompt$
But this doesn't work:
bash-prompt$ sage -c "attach somefile.sage; print 3"
python: can't open file '/path/to/somefile.sage; print Integer(3)': [Errno 2] No such file or directory
How can I get this to work, or what can I do instead?

If it helps someone...
I ended up using this monstrosity:
sage somefile.sage && sage -python -c "execfile('somefile.py'); wrapper()" && rm somefile.py
Ok, the rm part is not really all that necessary :)

I think the easiest thing to do is to call the function as the last line in the actual Sage file: that is, "somefile.sage" will have as its last line print 3, for example.

Related

Error "tput No value for $TERM and no -T specified"

I have a shell script in server A and I have a shell script in server B.
My logic is written like shell script in A gets executed and it calls a shell script in server B and executes it.
I am able to get the desired result when A executes B, but getting an error also along with the result. Error Message:
tput: No value for $TERM and no -T specified
I am using the following lines for getting output in color;
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 6`
BOLD=`tput bold`
RESET=`tput sgr0`
These lines are available in the shell script in both A and B.
When I execute the shell script in B by logging into server B, the desired output comes along with the color.
When I call the shell script from A and execute it, I am getting desired result plus the error message which I mentioned above.
Can you help in this regard?
FYI, I checked "echo $TERM" and output is 'xterm' in both the servers.
Not sure where I am going wrong.
My solution:
# when $TERM is empty (non-interactive shell), then expand tput with '-T xterm-256color'
[[ ${TERM}=="" ]] && TPUTTERM='-T xterm-256color' \
|| TPUTTERM=''
declare -r RES='tput${TPUTTERM} sgr0' REV='tput${TPUTTERM} rev'
declare -r fRD='tput${TPUTTERM} setaf 1' bRD='tput${TPUTTERM} setab 1'
declare -r fGN='tput${TPUTTERM} setaf 2' bGN='tput${TPUTTERM} setab 2'
...
echo ${fRD}" RED Message: ${REV} This message is RED REVERSE. "${RES}
echo ${fGN}" GREEN Message: ${REV} This message is GREEN REVERSE. "${RES}
...
This way it makes no sense if there's an interactive or a non-interactive shell - tput still works fine.
$TERM will be unset when you log in via a script. Bypass the color-coding in this scenario, or hard-code an option to tput. (I would strongly suggest the former.)
RED=; GREEN=; YELLOW=; BLUE=; BOLD=; RESET=;
case ${TERM} in
'') ;;
*)
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 6`
BOLD=`tput bold`
RESET=`tput sgr0`;;
esac
We supposed that the shell or environment setting are different in two servers, if possible, you can past output of next commands from the two servers.
ps
This command helps us understand what kind of shell we are using.
env
This command helps us know the environment.
I am able to fix the issue successfully now.
ssh <Server B> "TERM=xterm" <script in ServerB>
The desired output came back to ServerA with colors.

Including some SFTP commands in a Makefile

I use a Makefile to create pdfs of papers I'm working on. I'd also like to use make to upload the latest version to my website, which requires sftp. I though I could do something like this (which words on the command line) but it seems that in make, the EOF is getting ignored i.e., this
website:
sftp -oPort=2222 me#mywebsite.com << EOF
cd papers
put research_paper.pdf
EOF
generates an error message
cd papers
/bin/sh: line 0: cd: papers: No such file or directory
which I think is saying "papers" doesn't exist on your local machine i.e., the 'cd' is being executed locally, not remotely.
Couple of ideas:
use ncftp which every Linux distro as well as brew should have: it remembers 'state' so the cd becomes unnecessary
use scp instead of sftp if possible
write a trivial shell script doing the EOF business and call that
For what it is worth, here is my script to push tarballs to the CRAN winbuilder -- and takes target directory and script as arguments to ncftpput.
#!/bin/bash
function errorexit () {
echo "Error: $1"
exit 1
}
if [ "$#" -lt 1 ]; then
errorexit "Need to specify argument file"
fi
if [ ! -f ${1} ]; then
errorexit "File ${1} not found, aborting."
fi
ncftpput win-builder.r-project.org /R-release ${1}
ncftpput win-builder.r-project.org /R-devel ${1}
I then just do wbput.sh foo_1.2-3.tar.gz and off it goes...
You cannot (normally) put a single command on multiple lines in a Make recipe, so here documents are a no-go. Try this instead:
website: research_paper.pdf
printf 'cd papers\nput $<\n' \
| sftp -oPort=2222 me#mywebsite.com
The target obviously depends on the PDF, so I made it an explicit dependency, as well.

ksh error remove from list

I am trying to remove a certain element from a list in korn shell. It's working on my linux machine but the exact same code gives me an error on a solaris11 machine. I need a code that will work for both. It's probably because of different ksh versions but I would like to find a solution that works for both.
The code is:
#!/bin/ksh
MY_LIST="HELLO HOW ARE YOU"
toDel="HOW"
MY_LIST=( "${MY_LIST[#]/$toDel}" )
echo "MY LIST AFTER REMOVING HOW IS $MY_LIST"
On Solaris I get the following error:
syntax error at line 4 : '(' unexpected
Any suggestions?
Melodie wrote: Finally, I used 'Walter A' solution
Nice I could help.
Enabling you to vote for me and close the question, I post my comment as an answer.
MY_LIST=`echo $MY_LIST | sed "s/$toDel//"`
You'll probably need to spend some time with the ksh88 man page.
Without futher explanation:
set -A my_list HELLO HOW ARE YOU # note, separate words
toDel=HOW
set -- # using positional parameters as "temp array"
for word in "${my_list[#]}"; do
[[ $word != $toDel ]] && set -- "$#" "$word"
done
set -A my_list "$#"
printf "%s\n" "${my_list[#]}"
HELLO
ARE
YOU
Finally, I used 'Walter A' solution:
MY_LIST=`echo $MY_LIST | sed "s/$toDel//"`

Script response if md5sum returns FAILED

Say I had a script that checked honeypot locations using md5sum.
#!/bin/bash
#cryptocheck.sh
#Designed to check md5 CRC's of honeypot files located throughout the filesystem.
#Must develop file with specific hashes and create crypto.chk using following command:
#/opt/bin/md5sum * > crypto.chk
#After creating file, copy honeypot folder out to specific folders
locations=("/share/ConfData" "/share/ConfData/Archive" "/share/ConfData/Application"
"/share/ConfData/Graphics")
for i in "${locations[#]}"
do
cd "$i/aaaCryptoAudit"
/opt/bin/md5sum -c /share/homes/admin/crypto.chk
done
And the output looked like this:
http://pastebin.com/b4AU4s6k
Where would you start to try and recognize the output and perhaps trigger some sort of response by the system if there is a 'FAILED'?
I've worked a bit with PERL trying to parse log files before but my attempts typically failed miserably for one reason or another.
This may not be the proper way to go about this, but I'd want to be putting this script into a cronjob that would run every minute. I had some guys telling me that an inotify job or script (I'm not familiar with this) would be better than doing it this way.
Any suggestions?
--- edit
I made another script to call the script above and send the output to a file. The new script then runs a grep -q on 'FAILED' and if it picks anything up, it sounds the alarm (tbd what the alarm will be).
#!/bin/bash
#cryptocheckinit.sh
#
#rm /share/homes/admin/cryptoalert.warn
/share/homes/admin/cryptocheck.sh > /share/homes/admin/cryptoalert.warn
grep -q "FAILED" /share/homes/admin/cryptoalert.warn && echo "LIGHT THE SIGNAL FIRES"
Use:
if ! /opt/bin/md5sum -c /share/homes/admin/crypto.chk
then
# Do something
fi
Or pipe the output of the loop:
for i in "${locations[#]}"
do
cd "$i/aaaCryptoAudit"
/opt/bin/md5sum -c /share/homes/admin/crypto.chk
done | grep -q FAILED && echo "LIGHT THE SIGNAL FIRES"

run Matlab in batch mode

It seems to me that there are two ways to run Matlab in batch mode:
the first one:
unset DISPLAY
matlab > matlab.out 2>&1 << EOF
plot(1:10)
print file
exit
EOF
The second one uses option "-r MATLAB_command":
matlab -nojvm -nosplash -r MyCommand
Are these two equivalent?
What does "<< EOF" and the last "EOF" mean in the first method?
Thanks and regards!
The first method simply redirects the standard output > matlab.out and the standard error 2>&1 to the file matlab.out.
Then it uses the heredoc way of passing input to MATLAB (this is not specific to MATLAB, it is a method of passing multiple lines as input to command line programs in general).
The syntax is << followed by an unique identifier, then your text, finally the unique id to finish.
You can try this on the shell:
cat << END
some
text
multiple lines
END
The second method of using the -r option starts MATLAB and execute the statement passed immediately. It could be some commands or the name of a script or function found on the path.
It is equivalent to doing something like:
python -c "print 'hello world'"
Refer to this page for a list of the other start options.