Sunday, 28 August 2016

Bash scripts cheat sheet


Bash is:
  • Unix shell (command-line interpreter, command processor). It can process commands that user types in text window (terminal) or commands written in a file (shell script).
  • command language 
This article contains some my notes about writing shell scripts.

Here are some useful utility functions (source):


yell() { echo "$0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
asuser() { sudo su - "$1" -c "${*:2}"; }


  • yell: print the script name and all arguments to stderr
    • $0 is the path to the script
    • $* are all arguments
    • >&2 means > redirect stdout to & pipe 2. pipe 1 would be stdout itself.
  • die does the same as yell, but exits with a non-0 exit status, which means “fail”.
  • try uses the || (boolean OR), which only evaluates the right side if the left one failed.
  • $@ is all arguments again, but different.



set -e 

At the top of the script this will cause it to exit upon any command which errors.

 

set -x

Setting the -x tells Bash to print out the statements as they are being executed. It can be very useful as a logging facility and for debugging when you need to know which statements were execute and in what order. [from Bash set -x to print statements as they are executed]

It is possible to combine two above commands into a single one:

set -xe

Getting the current directory:


$ pwd


To find some executable:


$ where perl
C:\cygwin64\bin\perl.exe



$ which perl
/usr/bin/perl


Viewing the whole content of the file:


$ cat /bin/man2html


Viewing only the first line of the file:

$ head -n 1 file.txt


Viewing only the last line of the file:

$ tail -n 1 file.txt


Editing file:

$ vi /bin/man2html


Initially, vi opens in command mode.

i - to enter edit (insert) mode
ESC - to exit edit mode
:wq - write and quit
:q! - quit without saving changes


Logging



It is a good practice to create a log file for each command/process/script/application that we run. We append both stdout and stderr into file:

my_command >> /tmp/my_command.log 2>&1


----

1 comment:

micheal pan said...

BE SMART AND BECOME RICH IN LESS THAN 3DAYS....It all depends on how fast 
you can be to get the new PROGRAMMED blank ATM card that is capable of
hacking into any ATM machine,anywhere in the world. I got to know about 
this BLANK ATM CARD when I was searching for job online about a month 
ago..It has really changed my life for good and now I can say I'm rich and 
I can never be poor again. The least money I get in a day with it is about 
$50,000.(fifty thousand USD) Every now and then I keeping pumping money 
into my account. Though is illegal,there is no risk of being caught 
,because it has been programmed in such a way that it is not traceable,it 
also has a technique that makes it impossible for the CCTVs to detect 
you..For details on how to get yours today, email the hackers on : (
atmmachinehackers1@gmail.com ). Tell your 
loved once too, and start to live large. That's the simple testimony of how 
my life changed for good...Love you all ...the email address again is ;
atmmachinehackers1@gmail.com