Re: will this script *always* work and pick out the file name?




Ed Morton wrote:
RolandRB wrote:
I know there are better ways of doing this but I have to stick with
something simple. I write shell scripts for clinical trials programmers
and at some stage they will have to maintain them so I can't do
anything fancy like write a C program. Will this *always* pick out the
file name, no matter what flavor of Linux it is or how big the file is
or if the filename has spaces in it?


#!/bin/bash

# Define give-usage-message-then-exit function
usage () {
echo 'Usage: myfiles *.sas' 1>&2
exit 1
}

valueu=$(whoami) # Default value for User

# "case" statement for action to take on selected options
while getopts "u:" param ; do
case $param in
"?") # bad option supplied
usage
;;
"u") # (value) User
valueu=$OPTARG
;;
esac
done


# shift to bring first parameter to position 1
shift $(($OPTIND - 1))


if [ $# -lt 1 ]
then
ls -lp | gawk '$3==userid' userid="$valueu" | \
gawk '{print substr(substr($0,25),index(substr($0,25),$6)+13)}'
else
ls -ldp -- "$@" | gawk '$3==userid' userid="$valueu" | \
gawk '{print substr(substr($0,25),index(substr($0,25),$6)+13)}'
fi



No, it'll fail for files with newlines in their names. Also, you don't
need to call gawk twice. This:

gawk '$3==userid' userid="$valueu" |
gawk '{print substr(substr($0,25),index(substr($0,25),$6)+13)}'

can just be this exactly equivalent:

gawk '$3==userid{print
substr(substr($0,25),index(substr($0,25),$6)+13)}' userid="$valueu"

Regards,

Ed.

Thanks (newlines not allowed in their file names).

.



Relevant Pages