Glob characters
* - means 'match any number of characters'. '/' is not matched
? - means 'match any single character'
[abc] - match any of the characters listed. This syntax also supports ranges, like [0-9]
Below is a simple script which explains the problem and also provide the solution using globbing:
Source:
cat glob.sh
#!/bin/bash
echo "Creating 2 files with space: this is first file.glob and another file with name.glob "
`touch "this is first file.glob"`
`touch "another file with name.glob"`
echo "---------------------------------"
echo "Using for loop to search and delete file ... failed :( "
for file in `ls *.glob`; do
echo $file
`rm $file`
done
echo "-------------------------------"
echo
echo "Using the another for loop to search for the same set of files ... but failed :("
for filename in "$(ls *.glob)"; do
echo $filename
`rm $filename`
done
# This is the example of using glob in the script
# This time BASH does know that it's dealing with filenames
echo "-------------------------------"
echo "Finnaly able to delete the file using glob method. :)"
for filename in *.glob; do
echo "$filename"
`rm "$filename"`
done
cat glob.sh
#!/bin/bash
echo "Creating 2 files with space: this is first file.glob and another file with name.glob "
`touch "this is first file.glob"`
`touch "another file with name.glob"`
echo "---------------------------------"
echo "Using for loop to search and delete file ... failed :( "
for file in `ls *.glob`; do
echo $file
`rm $file`
done
echo "-------------------------------"
echo
echo "Using the another for loop to search for the same set of files ... but failed :("
for filename in "$(ls *.glob)"; do
echo $filename
`rm $filename`
done
# This is the example of using glob in the script
# This time BASH does know that it's dealing with filenames
echo "-------------------------------"
echo "Finnaly able to delete the file using glob method. :)"
for filename in *.glob; do
echo "$filename"
`rm "$filename"`
done
Output:
./glob.sh
Creating 2 files with space: this is first file.glob and another file with name.glob
---------------------------------
Using for loop to search and delete file ... failed :(
another
rm: cannot remove `another': No such file or directory
file
rm: cannot remove `file': No such file or directory
with
rm: cannot remove `with': No such file or directory
name.glob
rm: cannot remove `name.glob': No such file or directory
this
rm: cannot remove `this': No such file or directory
is
rm: cannot remove `is': No such file or directory
first
rm: cannot remove `first': No such file or directory
file.glob
rm: cannot remove `file.glob': No such file or directory
-------------------------------
Using the another for loop to search for the same set of files ... but failed :(
another file with name.glob this is first file.glob
rm: cannot remove `another': No such file or directory
rm: cannot remove `file': No such file or directory
rm: cannot remove `with': No such file or directory
rm: cannot remove `name.glob': No such file or directory
rm: cannot remove `this': No such file or directory
rm: cannot remove `is': No such file or directory
rm: cannot remove `first': No such file or directory
rm: cannot remove `file.glob': No such file or directory
-------------------------------
Finnaly able to delete the file using glob method. :)
another file with name.glob
this is first file.glob
0 comments:
Post a Comment