Testing For File Charactereristics

commandline session

$cat test.sh
#!/usr/bin/env bash
# cookbook filename: checkfile
#
DIRPLACE=/tmp
INFILE=/home/jeffrin/amazing.data
OUTFILE=/home/jeffrin/more.results
if [ -d "$DIRPLACE" ]
then
cd $DIRPLACE
if [ -e "$INFILE" ]
then
if [ -w "$OUTFILE" ]
then
doscience > "$OUTFILE"
else
echo "can not write to $OUTFILE"
fi
else
echo "can not read from $INFILE"
fi
else
echo "can not cd into $DIRPLACE"
fi

$sh test.sh
can not read from /home/jeffrin/amazing.data
$> /home/jeffrin/amazing.data
$sh test.sh
can not write to /home/jeffrin/more.results
$> /home/jeffrin/more.results
$sh test.sh
test.sh: 14: test.sh: doscience: not found
$

related source : Bash CookBook

Bash . Running Several Commands All at Once

commandline session

$ls
src  war
$cd & ls &
[1] 2587
[2] 2588
$src  war

[1]-  Done                    cd
[2]+  Done                    ls --color=auto
$pwd
/home/jeffrin/beautiful-work
$
$cd & ls & cd &
[1] 2590
[2] 2591
[3] 2592
$src  war

[1]   Done                    cd
[2]-  Done                    ls --color=auto
[3]+  Done                    cd
$pwd
/home/jeffrin/beautiful-work
$cd .. & pwd  &
[1] 2593
[2] 2594
$/home/jeffrin/beautiful-work

[1]-  Done                    cd ..
[2]+  Done                    pwd
$pwd
/home/jeffrin/beautiful-work
$cd &
[1] 2614
$pwd
/home/jeffrin/beautiful-work
[1]+  Done                    cd
$ls & cd ; pwd
[1] 2625
/home/jeffrin
$src  war

[1]+  Done                    ls --color=auto  (wd: ~/beautiful-work)
(wd now: ~)
$pwd
/home/jeffrin
$

Basic tinkering with “set” command and “noclobber” related

commandline session

[bash light=”true”]
$echo improve > clean.txt
$cat clean.txt
improve
$set -o noclobber
$echo new > clean.txt
bash: clean.txt: cannot overwrite existing file
$echo new >> clean.txt
$cat clean.txt
improve
new
$echo again >| clean.txt
$cat clean.txt
again
$set +o noclobber
$echo improve > clean.txt
$cat clean.txt
improve
$
[/bash]

Errors and Exceptions

commandline session

$gcc error.c
$cat error.c
#include

main()
{
int a=0;
int b=10;
printf("Hello Alln");
printf("%d",b/a);
}

$./a.out > outputa
Floating point exception
$cat outputa
$
$./a.out 2> outputa
Hello All
Floating point exception
$./a.out > outputa
Floating point exception
$./a.out 2> outputb
Hello All
Floating point exception
$./a.out &> outputc
Floating point exception
$cat outputa
$
$cat outputb
$cat outputc
$

An Error “indicates serious problems that a reasonable application should not try to catch.

An Exception “indicates conditions that a reasonable application might want to catch.

source : http://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions