#!/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
This is Unix and Scripting tools to solve this problem. Your efforts are appreciated. Thank you very much
Please find the following shell script to match the file patterns given as part of scripts command line arguments.
Note: I have added comments inline and screen shots for better understanding.
You can copy , paste script in a new file named "glob.any.sh"
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$ :
Screen Shot
Sample Output Screen Shot:
#!/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...
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...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...