How do I display current dir if no dir is given

How do I display current dir if no dir is given

Post by C » Mon, 22 Apr 2002 13:28:14



Hello, I am trying to finish a script file for one of my class
projects, and I am having a hard time figuring out how to make my
current directory the default directory if one isn't given.  It would
go to the current dir until I added the getopts command.  Also how do
I make the * option come up if I don't select neither d nor f?
Example, script dir. Instead of script -d dir.  Again, this worked
until I added the getopts command.  If someone can help me it would be
appreciated.  Here is what I have done so far.  Just in case any of
you think I am trying to take the easy way out instead of actually
doing the work.

This script file should tell how many sub-directories and files are in
the specified directory.  If d or f is selected it will list the
permissions minus the first character, and the name of the file or
directory.  Both options can be used at the same time.

while getopts df dir
do

case $dir in

d)  ls -l $2 | cut -c1-10 | grep '^[d]' > dir

    ls -l $2 | cut -c1-10 | grep '^[-]' > file

    cat dir | sort | uniq -c | tr -d '[a-z] [-]' > dir1

    cat file | sort | uniq -c | tr -d '[a-z]' > file 1

    awk ' { total += $1 }
    END { print total, "sub-directories"}' < dir1

    awk ' { total += $1 }
    END { print total, "files"}' < file1

    ls -l $2 | cut -c1-10,54- | grep '^[d]' > info

    tr -d '[d]' < info > mess

    sed 's/ /    /g' < mess > mess1

    cat mess1
   ;;

f)  ls -l $2 | cut -c1-10 | grep '^[d]' > dir2

    ls -l $2 | cut -c1-10 | grep '^[-]' > file2

    cat dir2 | sort | uniq -c | tr -d '[a-z] [-]' > dir3

    cat file2 | sort | uniq -c | tr -d '[a-z]' > file3

    awk ' { total += $1 }
    END { print total, "sub-directories"}' < dir3

    awk ' { total += $1 }
    END { print total, "files"}' < file3

    ls -l $2 | cut -c1-10,54- | grep '^[-]' > info1

    sed 's/ /    /g' < info1 > filesep

    cut -c2-10 < filesep > filesep1

    cut -f2 < filesep > filesep2

    paste filesep1 filesep2 > filesep3

    cat filesep3
   ;;

*) ls -l $1 | cut -c1-10 | grep '^[d]' > dir

   ls -l $1 | cut -c1-10 | grep '^[-]' > file

   cat dir | sort | uniq -c | tr -d '[a-z] [-]' > dir1

   cat file | sort | uniq -c | tr -d '[a-z]' > file1

   awk  ' { total += $1 }
   END { print total, "sub-directories"}' < dir1

   awk ' { total += $1 }
   END { print total, "files"}' < file1
   ;;

esac
done

 
 
 

How do I display current dir if no dir is given

Post by Faux_Pseud » Mon, 22 Apr 2002 15:31:07


--(Once apon a time, in comp.unix.shell,)--
                --(CJ said it like only they can.)--

Quote:> Hello, I am trying to finish a script file for one of my class
> projects, and I am having a hard time figuring out how to make my
> current directory the default directory if one isn't given.  It would
> go to the current dir until I added the getopts command.  Also how do
> I make the * option come up if I don't select neither d nor f?
> Example, script dir. Instead of script -d dir.  Again, this worked
> until I added the getopts command.  If someone can help me it would be
> appreciated.  Here is what I have done so far.  Just in case any of
> you think I am trying to take the easy way out instead of actually
> doing the work.

The first part

man bash:

     ${parameter:=word}
              Assign Default Values.  If parameter  is  unset  or
              null,  the expansion of word is assigned to parame-
              ter.  The value of parameter is  then  substituted.
              Positional  parameters  and  special parameters may
              not be assigned to in this way.

Thus we have the following:


+-($:~)-
unset dir ;  ${dir:=$PWD} 2>/dev/null ;  echo $dir
/home/faux

+-($:~)-
dir=foo_bar ;  ${dir:=$PWD} 2>/dev/null ;  echo $dir
foo_bar

The second part:

usage() {
    cat >&1 <<-EOM
Usage for $(basename $0):
-t test script
-h print this help message (like you didn't know)

            Written by Faux_Pseudo at yahoo com
EOM

Quote:}

if [[ $# < 1 ]] ; then usage ; exit 1 ; fi

while getopts th o
  do case $o
  in
      t) echo worked
              ;;
      h) usage
              ;;
      *) usage
              ;;
  esac
done

Its all in the hand book. --Beetlejuice

--

It's a damn poor mind that can only think of one way to spell a word.
        - Andrew Jackson
UIN=66618055

 
 
 

How do I display current dir if no dir is given

Post by Chris F.A. Johnso » Tue, 23 Apr 2002 05:28:56



> Hello, I am trying to finish a script file for one of my class
> projects, and I am having a hard time figuring out how to make my
> current directory the default directory if one isn't given.  It would
> go to the current dir until I added the getopts command.  Also how do
> I make the * option come up if I don't select neither d nor f?
> Example, script dir. Instead of script -d dir.  Again, this worked
> until I added the getopts command.  If someone can help me it would be
> appreciated.  Here is what I have done so far.  Just in case any of
> you think I am trying to take the easy way out instead of actually
> doing the work.

> This script file should tell how many sub-directories and files are in
> the specified directory.  If d or f is selected it will list the
> permissions minus the first character, and the name of the file or
> directory.  Both options can be used at the same time.

> while getopts df dir
> do

> case $dir in

> d)  ls -l $2 | cut -c1-10 | grep '^[d]' > dir

>     ls -l $2 | cut -c1-10 | grep '^[-]' > file

>     cat dir | sort | uniq -c | tr -d '[a-z] [-]' > dir1

>     cat file | sort | uniq -c | tr -d '[a-z]' > file 1

>     awk ' { total += $1 }
>     END { print total, "sub-directories"}' < dir1

>     awk ' { total += $1 }
>     END { print total, "files"}' < file1

>     ls -l $2 | cut -c1-10,54- | grep '^[d]' > info

>     tr -d '[d]' < info > mess

>     sed 's/ /    /g' < mess > mess1

>     cat mess1
>    ;;

> f)  ls -l $2 | cut -c1-10 | grep '^[d]' > dir2

>     ls -l $2 | cut -c1-10 | grep '^[-]' > file2

>     cat dir2 | sort | uniq -c | tr -d '[a-z] [-]' > dir3

>     cat file2 | sort | uniq -c | tr -d '[a-z]' > file3

>     awk ' { total += $1 }
>     END { print total, "sub-directories"}' < dir3

>     awk ' { total += $1 }
>     END { print total, "files"}' < file3

>     ls -l $2 | cut -c1-10,54- | grep '^[-]' > info1

>     sed 's/ /    /g' < info1 > filesep

>     cut -c2-10 < filesep > filesep1

>     cut -f2 < filesep > filesep2

>     paste filesep1 filesep2 > filesep3

>     cat filesep3
>    ;;

> *) ls -l $1 | cut -c1-10 | grep '^[d]' > dir

>    ls -l $1 | cut -c1-10 | grep '^[-]' > file

>    cat dir | sort | uniq -c | tr -d '[a-z] [-]' > dir1

>    cat file | sort | uniq -c | tr -d '[a-z]' > file1

>    awk  ' { total += $1 }
>    END { print total, "sub-directories"}' < dir1

>    awk ' { total += $1 }
>    END { print total, "files"}' < file1
>    ;;

> esac
> done

    You have so much unnecessary code that it's too much work to
    figure out exactly what you're doing.

    When you use getopts, you should set flags rather than try to run
    your code within the loop:

         d=0
         f=0
         while getopts df dir
         do
            case $dir in
               d) d=1 ;;
               f) f=1 ;;
         done
         shift $(( $OPTIND - 1 ))

    Now, any remaining argument is the directory to be examined. If none
    is specified, use the current directory:

          ls -l "${1:-.}"

    The information you want to display, depends on the options
    selected, and will include:

        Number of subdirectories:

           dnum=`ls -l "${1:-.}" | grep -c ^d`

        Number of files:

           fnum=`ls -l "${1:-.}" | grep -c ^-`

        List of files:

           flist=`ls -l "${1:-.}" | grep ^-``

        List of directories:

           dlist=`ls -l "${1:-.}" | grep ^d`

    Of course, you don't need to call ls four times; just store the
    whole list in a variable and pipe that through the different grep
    commands:

       list=`ls -ln "${1:-*}"`
       dnum=`echo "$list" | grep -c ^d`
       fnum=.......etc.

    Then print the information you want based on the values of
    $d and $f.

    To delete the first character from each line of the listing, pipe
    it through cut:

        echo "$dlist" | cut -c2-

    If you want to extract information from a long listing, you can
    do it like this:

        echo "$dlist" |
           while read perms _ _ _ size month day time file link
           do
              ## print the information you want, e.g.:
              echo "$perms $file"
           done

--

        =================================================================