Question

#!/bin/sh [ $# -ge 1 ] || {    cat <<- EOF        NAME   ...

#!/bin/sh

[ $# -ge 1 ] || {
   cat <<- EOF
       NAME
       globs.all.sh - displays entries in the current directory that match all of the given globs

       SYNOPSIS
       $(basename $0) glob[...]

       DESCRIPTION
       Displays the names in the current directory that match ALL of the given globs.
       Each file is displayed at most once.

       EXIT STATUS
       0 if any name matched all globs
       1 if no name matched all globs
       2 if no glob given

       NOTES
       use case in to do the glob matching

       EXAMPLES

       \$ $(basename $0) '*'
       123
       abc
       abc123
       xxx

       \$ $(basename $0) '*c*' '*1*'
       abc123

   EOF
   exit 2
} >&2
Using Unix and Scripting tools to solve this problems. Your efforts are appreciated. Thank you very much

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#!/bin/sh

[ $# -ge 1 ] || {
cat <<-EOF
NAME
globs.any.sh - displays entries in the current directory that match any of the given globs

SYNOPSIS
$(basename $0) glob[...]

DESCRIPTION
Displays the names in the current directory that match ANY of the given globs.
Each file is displayed at most once.

EXIT STATUS
0 if any name matched any glob
1 if no name matched any glob
2 if no glob given

NOTES
use case in to do the glob matching

EXAMPLES

\$ $(basename $0) '*'
123
abc
abc123
xxx

\$ $(basename $0) '*c*' '*1*' '*z*'
123
abc
abc123

EOF
exit 2
} >&2

#execute ls pattern, to check whether we have any file macthes the glob
#$* store all the parameters passed to the shell script
#output of ls command will not be shown in the console

file=""
total=0
pattern=""

for pattern in $*
do
ls $pattern >/dev/null 2>&1
if [ $? == 0 ]
then
total=$((total+1))
#append the file list in variable "file"
file=$file" "$pattern
fi
done


#if total is more than 0, then exit with 0, else, exit 1
if [ $total -ge 1 ]
then
#display the list of files in one in a line
echo $file|tr " " "\n"
#exit with the ret value
exit 0
else
exit 1
fi

Sample Output:

We can use "echo $?" to verify the return/exit value of the script

osboxes@osboxes:~/Chegg/SHELL$ ./glob.sh liiffsf*
osboxes@osboxes:~/Chegg/SHELL$ echo $?
1
osboxes@osboxes:~/Chegg/SHELL$ ./glob.sh
NAME
globs.any.sh - displays entries in the current directory that match any of the given globs

SYNOPSIS
glob.sh glob[...]

DESCRIPTION
Displays the names in the current directory that match ANY of the given globs.
Each file is displayed at most once.

EXIT STATUS
0 if any name matched any glob
1 if no name matched any glob
2 if no glob given

NOTES
use case in to do the glob matching

EXAMPLES

$ glob.sh '*'
123
abc
abc123
xxx

$ glob.sh '*c*' '*1*' '*z*'
123
abc
abc123

osboxes@osboxes:~/Chegg/SHELL$ echo $?
2

osboxes@osboxes:~/Chegg/SHELL$ ./glob.sh 1*
1test
osboxes@osboxes:~/Chegg/SHELL$ echo $?
0
osboxes@osboxes:~/Chegg/SHELL$ ./glob.sh 1 1231232
1test
osboxes@osboxes:~/Chegg/SHELL$ echo $?
0
osboxes@osboxes:~/Chegg/SHELL$ ./glob.sh 1231232*
osboxes@osboxes:~/Chegg/SHELL$ echo $?
1
osboxes@osboxes:~/Chegg/SHELL$ :

output:

Add a comment
Know the answer?
Add Answer to:
#!/bin/sh [ $# -ge 1 ] || {    cat <<- EOF        NAME   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #!/bin/sh [ $# -ge 1 ] || {    cat <<- EOF        NAME   ...

    #!/bin/sh [ $# -ge 1 ] || {    cat <<- EOF        NAME        globs.any.sh - displays entries in the current directory that match any of the given globs        SYNOPSIS        $(basename $0) glob[...]        DESCRIPTION        Displays the names in the current directory that match ANY of the given globs.        Each file is displayed at most once.        EXIT STATUS        0 if any name matched any...

  • LUNIX (Please Label) Exit vi (:q) and from the command line, type viscript4.sh. For this script,...

    LUNIX (Please Label) Exit vi (:q) and from the command line, type viscript4.sh. For this script, we will iterate through all files in the current directory print out their name using the for loop. Example (do not type yet): foriin*;do …;done where the…does some operation on$I, which stands for the current file. To do this, enter the following in your script4.sh file:           #!/bin/bash           for i in *; do                echo $i           done Once the above works, change the for loop to...

  • Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple...

    Writing Unix Utilities in C (not C++ or C#) my-cat The program my-cat is a simple program. Generally, it reads a file as specified by the user and prints its contents. A typical usage is as follows, in which the user wants to see the contents of my-cat.c, and thus types: prompt> ./my-cat my-cat.c #include <stdio.h> ... As shown, my-cat reads the file my-cat.c and prints out its contents. The "./" before the my-cat above is a UNIX thing; it...

  • Can you comment notes along with the code so I can follow along and understand what is being written?? Thanks in advance...

    Can you comment notes along with the code so I can follow along and understand what is being written?? Thanks in advance! Let's say we're in a directory and we want to rename every file ending in·c, to end in .C (i.e., capitalize the c'). Here's one way to do it: $ for i in *. c; b-basename "$1" .c'; mv "8b.c" do "8b.C". done Things get more complicated if you want more complex renaming. Say for example you just...

  • The first script is validate.sh. This is a simple form validation script that will be used...

    The first script is validate.sh. This is a simple form validation script that will be used to verify the inputs given. Normally, this would be done to validate input from a website or another program before entry into a database or other record storage. In this case, we will keep it simple and only focus on the input validation step. In particular, the script should prompt the user for four values: first name, last name, zip code, and email address....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT