Using octave headless - terminal

Is there a possibility to use Octave headless.
Something like this
octave < "5+4" >result.txt

Using
octave --silent --eval 5+4 > result.txt
you'll get
ans = 9
in result.txt. See octave --help for details about command-line arguments.
Yet, there is this infamous ans = that might be remove using sed, e.g.
octave --silent --eval 'x=5+4; y=x+1; disp(y)' | sed -e 's/ans = //' >> result.txt
which add the appropriate result (10) in result.txt.
It should not be too hard to wrap this into a bash script.

Well there is always the option of writing a script file which saves the results of your computations to a text file. Then when invoking octave you just do:
octave scriptname.m
for example:
testfile.m
Return = 5+4;
save('results.txt','Return')
Then from the command line:
octave -q testfile.m
and you should get the results you want in a file called results.txt and it will immediately terminate after. Is there some reason why this option wont work?

Related

Ruby System command isn't taking `echo -n`

I'm trying to use the system command to output text to a file. I'm using echo -n and passing -n so it doesn't automatically create a newline in the file. Here's the command I'm using:
system("echo -n \"text for the file\" >> path/to/file.txt")
When I check the text file, the output that I get is:
-n text for file
If I run that same exact command in my terminal, I don't get the -n
Why is it not accepting the -n as an argument?
echo -n is not portable. Use printf instead to print the string.
system("printf 'text of the file' >> path/to/file.txt")
Specifically, the POSIX specification allows a conforming implementation of echo to support a -n option, but does not require it or define what it should do. Your system call is using a shell (probably dash) that does not implement -n and treats it as a regular argument to write to standard output.
Alongside, chepner's useful answer, since the system() call calling the native implementation of echo which does not implement the -n functionality, you could call the FreeBSD or a GNU based installation of echo installed explicitly as below,
In Mac OS the FreeBSD version of echo is installed as /bin/echo which you can use as
system("/bin/echo -n \"text for the file\" >> path/to/file.txt")
As a side-note, you could first see if echo is installed by doing which echo before running this.

gnome terminal tabs open multiple ssh connections

I have a file with a list of servers:
SERVERS.TXT:
192.168.0.100
192.168.0.101
192.168.0.102
From a gnome terminal script, I want open a new terminal, with a tab for each server.
Here is what I tried:
gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e 'ssh usr#$SERVER'"; done < SERVERS.TXT`
Here is the error:
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was ''ssh')
Tried removing the space after the -e
gnome-terminal --profile=TabProfile `while read SERVER ; do echo "--tab -e'ssh usr#$SERVER'"; done < SERVERS.TXT`
And I get a similar error:
Failed to parse arguments: Argument to "--command/-e" is not a valid command: Text ended before matching quote was found for '. (The text was 'usr#192.168.0.100'')
Obviously there is a parsing error since the the shell is trying to be helpful by using the spaces to predict and place delimiters. The server file is changed without notice and many different sets of servers need to be looked at.
I found this question while searching for an answer to the issue the OP had, but my issue was a little different. I knew the list of servers, they where not in a file.
Anyway, the other solutions posted did not work for me, but the following script does work, and is what I use to get around the "--command/-e" is not a valid command" error.
The script should be very easy change to suit any need:
#!/bin/sh
# Open a terminal to each of the servers
#
# The list of servers
LIST="server1.info server2.info server3.info server4.info"
cmdssh=`which ssh`
for s in $LIST
do
title=`echo -n "${s}" | sed 's/^\(.\)/\U\1/'`
args="${args} --tab --title=\"$title\" --command=\"${cmdssh} ${s}.com\""
done
tmpfile=`mktemp`
echo "gnome-terminal${args}" > $tmpfile
chmod 744 $tmpfile
. $tmpfile
rm $tmpfile
Now the big question is why does this work when run from a file, but not from within a script. Sure, the issue is about the escaping of the --command part, but everything I tried failed unless exported to a temp file.
I would try something like:
$ while read SERVER;do echo -n "--tab -e 'ssh usr#$SERVER' "; \
done < SERVERS.txt | xargs gnome-terminal --profile=TabProfile
This is to avoid any interpretation that the shell could do of the parameters (anything starting with a dash).
Because it is concatenating strings (using -n), it is necessary to add an space between them.
Is this a problem of parsing command-line options? Sometimes if you have one command sending arguments to another command, the first can get confused. The convention is to use a -- like so:
echo -- "--tab -e 'ssh usr#$SERVER'";
Try to type
eval
before gnome terminal command.
it should be something like this:
eval /usr/bin/gnome-terminal $xargs
worked for me!

Copy current command at bash prompt to clipboard

I would like a quick keyboard command sequence to copy the current command at a bash prompt to the clipboard.
So that, for example, to copy the last bash command to the clipboard, I'd press up+[some command sequence] to copy it. Or, for example, to search for a command in bash hisory, I'd use ctrl+r, search, display it on the command prompt, and then [some command sequence] to copy it, etc.
My current solution is using bash pipes: Pipe to/from the clipboard
So, to copy the previous command to clipboard:
echo "!!" | pbcopy
Which isn't too terrible, but what if the command to copy isn't the last command, etc.
What's the proper way to achieve what I'm trying to achieve here?
Taking #Lauri's post for inspiration, here's a solution using the bind command:
bind '"\C-]":"\C-e\C-u pbcopy <<"EOF"\n\C-y\nEOF\n"'
ctrl-] then will copy whatever is on the current bash prompt to the clipboard.
To make it persistent, you can add the bind command as above to your ~/.bashrc, or you can strip off the outer quotes and remove the 'bind' part of the call and add the result to your ~/.inputrc.
Non-OS-X users will have to swap pbcopy out with the appropriate command, probably xclip.
A quoted heredoc was used instead of a an echo+pipe technique so that both single and double quotes in the command at the bash prompt are preserved. With this technique, for example, I was able to hit ctrl-], copy the actual bind command from the terminal prompt, and paste it here in the answer. So the heredoc technique handles all of the special characters in the bind command here.
You can use READLINE_LINE with bind -x in bash 4:
copyline() { printf %s "$READLINE_LINE"|pbcopy; }
bind -x '"\C-xc":copyline'
You can install bash 4 and make it the default login shell by running brew install bash;echo /usr/local/bin/bash|sudo tee -a /etc/shells;chsh -s /usr/local/bin/bash.
I also use this function to copy the last command:
cl() { history -p '!!'|tr -d \\n|pbcopy; }
I spent a decent amount of time today writing a simple zsh implementation for macOS; usage is as follows:
example command: git commit -m "Changed a few things"
command that copies: c git commit -m "Changed a few things"
# The second command does not actually execute the command, it just copies it.
# Using zsh, this should reduce the whole process to about 3 keystrokes:
#
# 1) CTRL + A (to go to the beginning of the line)
# 2) 'c' + ' '
# 3) ENTER
preexec() is a zsh hook function that gets called right when you press enter, but before the command actually executes.
Since zsh strips arguments of certain characters like ' " ', we will want to use preexec(), which allows you to access the unprocessed, original command.
Pseudocode goes like this:
1) Make sure the command has 'c ' in the beginning
2) If it does, copy the whole command, char by char, to a temp variable
3) Pipe the temp variable into pbcopy, macOS's copy buffer
Real code:
c() {} # you'll want this so that you don't get a command unrecognized error
preexec() {
tmp="";
if [ "${1:0:1}" = "c" ] && [ "${1:1:1}" = " " ] && [ "${1:2:1}" != " " ]; then
for (( i=2; i<${#1}; i++ )); do
tmp="${tmp}${1:$i:1}";
done
echo "$tmp" | pbcopy;
fi
}
Go ahead and stick the two aforementioned functions in your .zshrc file, or wherever you want (I put mine in a file in my .oh-my-zsh/custom directory).
If anyone has a more elegant solution, plz speak up.
Anything to avoid using the mouse.
If xsel is installed on your system you can add this in .inputrc :
C-]: '\C-e\C-ucat <<"EOF" | tr -d "\\n" | xsel -ib\n\C-y\nEOF\n'
Alternatively, if xclip is installed you could add this:
C-]: '\C-e\C-ucat <<"EOF" | tr -d "\\n" | xclip -se c\n\C-y\nEOF\n'
Notice: Used code from #Clayton's answer.
I use history to find the command number that I am looking for, then I do:
echo "!command_number" | xclip -in
$ history | cut -c 8- | tail -1 | pbcopy
or in .zshrc file add an alias
alias copy='history | cut -c 8- | tail -1 | pbcopy'

Shell script error on slackware based Linux OS

I have a shell script that works on Ubuntu and provides me an output as I desire. When I test the same on a slackware linux version, my script fails.
The script fails at:
dialog --title "Test" --gauge "Copying file." 6 100 < <(
rsync -a --progress test.tar.gz /media/sda1 |
unbuffer -p grep -o "[0-9]*%" |
unbuffer -p cut -f1 -d '%'
)
The error is:
Syntax error near unexpected token `<'
What could be different between the two operating systems that the script fails to execute?
The script executes successfully if I get rid of the dialog command and the brackets etc.
Most likely, you are trying to run a bash script with non-bash shell. Or with older bash version.
First, try running it through bash explicitly, i.e.:
bash script.sh
You should also fix your shebang to point at bash:
#!/bin/bash
[Update below]
The < <( ... ) notation is unique for bash and zsh. The syntax error is a clear sign it is not recognised by the slackware shell.
Either slackware does not use bash, or its version of bash is too old for this feature.
Check the value of $BASH_VERSION on both platforms.
A possible alternative for
cat < <(
...
...
)
could be:
cat <<< "$(
...
...
)"
This will work in bash, ksh93, and zsh, and has been around slightly longer.
UPDATE
Based on your feedback, I've looked at the actual pipeline you try to use here.
I believe it's your intention to use column 3 of the --progress output as input for the dialog graphical progress indicator.
I tried this with a directory with lots of small files. Are you aware that this percentage indicator is per file? With my small files, rsync gave only one update per file. As every single file was written in one go, all percentages were equal to 100%.

Generating a bash script with echo, problem with shebang line

I want to explain to some friends how to add multikey support to their linux systems at bootup but first I need them to make a bash script. I want to make a simple command for them to copy and paste and I'm testing out this command I made but it keeps throwing an error. Only when I add the shebang line which, well is important.
$ sudo echo -e "#!/bin/bash \nxmodmap \"keysym Alt_R = Multi_key\"" > /etc.init.d/multikey.sh
Any easy way to echo a shebang line?
Use the other quotes.
sudo echo -e '#!/bin/bash\nxmodmap "keysym Alt_R = Multi_key"'
If you want to impress your friends use here documents not echo strings :-)
~$ cat << EOF > /etc/init.d/multikey.sh
> #!/bin/bash
> xmodmap "keysym Alt_R = Multi_key"
> EOF

Resources