"Suan Chuah" [email protected] wrote:
Hi Michael, I extracted the below codes from lab4 and have put down the queries below (after long ______). Apologies as I am still figuring out how to use more interactive editing style (at least with some colors?).
#!/bin/bash
get file name from command line
check if file exists
if file doesn't exist, print error message
if [[ ! -f $1 ]] ______ why we put $1 here? $1 is the first argument? or what are we referring to? can we put $0?
then
1>&2 echo ERROR ______ what does &2 here mean?
exit 1
else
# if file exists, print 3rd line
head -n 3 $1 | tail -n 1
fi
Hi Suan
A better way of saying: 1>&2 echo ERROR
is echo ERROR > /dev/stderr
The first of these can be used by all Unixes/Linuxes, but is very unobvious syntactically; translation: take the output sent to stdout (file descriptor 1) and send it instead to stderr (file descriptor 2). I therefore prefer the second redirection > /dev/stderr, but it may not be not fully portable. In particular, it is present in Bash and Ksh, but it may be absent in standard Shell (sh). That said, it is present in Dash
Cheers
MichaelW