Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and searches) and we'll do our best to help. Keep in mind that your instructor might also be an LQ member.
Assume that your current directory contains the files 'labtest', 'labtest1', 'labtest2', 'labtest2a', 'labtest3', and 'labtest4'.
How do I issue a command to delete only the files 'labtest1' and 'labtest2' using one ambiguous pathname. (character class)
You use the rm command with an "ambiguous pathname" (a term I have never heard but which makes sense to me). The ambiguous pathname is the name of a file where some characters are expressed as character classes.
I suggest you review the concepts of filename expansion and character class from your course. These are fundamental concepts in the shell. You may also use the Bash resources in my signature.
For example, [1-4] means a character in the range of 1 to 4. labtest[1-4] means files that are named labtest1, labtest2, labtest3 or labtest4. You could call it an ambiguous pathname
Rather than a range like [1-4], you can write a character class like this: [acfgh] or [12], which means a single character out of the set (a c g g h) or the set (1 2).
build yourself a test bed using the examples as files by the same names, then do yourself a bash script, for easy quick editing, or command line it, changing the search patterns until your get the results you need. you can use echo in place of delete (rm) to see what you are picking out of the bunch of the files by similar names.
using a script, you can run it in the same dir as the files or give it the path to the dir to search in and run the script elsewhere.
Code:
#!/bin/bash
searchPath=
you can set the script to give you the output of what it is doing
Code:
set -xv
you can set this particular script to create the files by name for your learning purposes. this is set to have the script be ran in the same dir as the files are created in to look for the matching two filenames
Code:
#creates files by name to be tested against.
touch tabtest labtest1 labtest2 labtest2a labtest3 labtest4
you can and there are other ways to skin this cat but this is just one suggestion. put the names in an array then use that array to check against the filenames being read into the script.
Code:
#file names to be found put in an array.
patteren1=( labtest1 labtest2 )
using nested loops, (or two loops, one in a function) one to read in the files in question and another loop to check all of the files against what is in the array with the filenames in question one is looking for. using a function, because they are useful.
Code:
IsItTheOne?()
{
#using string manipulations to find matching patterns
for (( i = 0; $i < ${#patteren1[@]}; i++ ))
do
if [[ ${g##*/} = "${patteren1[$i]}" ]] ; then
echo "found it $g"
fi
done
}
Code:
for g in $(pwd)/* ; do
IsItTheOne? $g
done
if you were to put that together in working order you might find a glitch (Bug) that needs to be worked out.
this is just to demonstrate there is more than one way to get the same results in scripting and programming.
knowing loops and string manipulations. strings and bash
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.