Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Sunday, June 9, 2019

shell testing

I have never seen in my life a bash shell being covered by automated tests.

I have thought of using Java and Mockito and Junit5, but it's not very straightforward to run shells from Java (in 2019.... maybe in 2 years it will be normal).

But I think it would be an excellent idea.

This is an inspiring article https://www.leadingagile.com/2018/10/unit-testing-shell-scriptspart-one/

This is the shunit2 framework:

https://github.com/kward/shunit2/


Here the reference manual for shell scripting http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html but it's a bit too academic.

https://www.tldp.org/LDP/abs/html/index.html this one is richer of examples

PS shell scripting sucks

Thursday, November 1, 2018

Spring Shell is very cool

If you are fed up with UI-oriented applications (like, say, Messus (a.k.a. Nexus) ) you can always try to embed a Spring-powered shell, so as to have a powerful scripting cli.

Just follow this example

https://docs.spring.io/spring-shell/docs/2.0.0.RELEASE/reference/htmlsingle/

and in a few clicks you have a functional shell.

BEWARE: baeldung has an example, but it's based on the old API. You will search in vain for Bootstrap class under org/springframework/shell

spring-shell-starter
spring-shell-core
spring-shell-standard
spring-shell-standard-commands
spring-shell-shell1-adapter
spring-shell-jcommander-adapter
spring-shell-table



Wednesday, August 22, 2018

shell input validation

shell scripting STINKS, and to make it better we should at least make it more robust.

For instance by validating input parameters

Arrays are a great and simple way to achieve this.

array=(pippo pluto topolino)
printf '%s ' "${array[@]}"
printf  '\n'
value=puppo
if [[ ! " ${array[@]} " =~ " ${value} " ]]; then
  echo "array doesnt contain value $value"
fi





Thursday, November 16, 2017

set -euf -o pipefail

https://sipb.mit.edu/doc/safe-shell/

nice to read about shell scripting. DON'T USE SHELL, USE PYTHON INSTEAD.

As mentioned in the article, you can use https://docs.python.org/2/library/subprocess.html or also PLUMBUM "Never write shell scripts again"

https://google.github.io/styleguide/shell.xml "Shell should only be used for small utilities or simple wrapper scripts. "



Thursday, November 28, 2013

Plumbum: Never write shell scripts again

http://plumbum.readthedocs.org/en/latest/
I haven't evaluated it, but surely I am 100% in favor of writing shell scripts in Python...

ALL unix shells languages suck big time... I am 10 times more productive in Python (even being a beginner) than in bash or korn...

Monday, January 14, 2013

Shelled, Shell script editor in Eclipse

If you are stuck with Eclipse, especially on a VISTA 32 bit as I am, well then I can only sympathize with you, probably after a few days of use you have become so negative and aggressive that you wife divorced you and your children have joined a foster family.

Yet here is a nice add-in to make it easier to write Shell scripts:

http://sourceforge.net/projects/shelled/

http://sourceforge.net/apps/trac/shelled/wiki/Documentation/InstallGuide

To install with the Update mechanism (I tried downloading the zip but installation failed saying that some dependencies were missing, of course STINKING Eclipse dones't tell you which ones)

Help/

https://downloads.sourceforge.net/project/shelled/shelled/ShellEd%202.0.2/update

Sunday, December 9, 2012

Shell script, redirecting a script's output in a simple way

This is really the simplest way I could find: just embed the redirection thing in the script itself, live happily ever after.



main_function() {
        echo Ciao
        echo Miao
}



main_function > pippo.log 2>&1



If you want to see the output while executing:
LOG_FILE=bla.log
main_function | tee -a $LOG_FILE 2>&1


Tuesday, November 13, 2012

Cheat sheet on how to write shell scripts

at the to, put:
#!/bin/bash


generate timestamp: `date +"%Y%m%d_%H%M"`

to append each command output to a log file AND send to console :
command | tee -a mylog.log

checking result of last command (it's the $? ):
if [ $? -ne 0 ]
then
echo OK
else
echo KO
fi


to assign positional parameters:
set -- BLA
the result is that $1=BLA