script
Name Change
1. Create a script in your Lab05 folder named namechange. Create some files with common errors (spaces in the filename, a misspelling, etc.). The final script will run as follows: namechange -f find -r replace filename
2. Use getopts to read in options and do actions based on arguments. getopts should support the following options in the OPTSTRING
· -h
· call printHelp function and exit script
· -f
· option followed by argument specifying pattern to find in the filename
· -r
· option followed by argument specifying what to replace the found pattern with in the filename
· \?
· use as case when option is not part of OPTSTRING
· call printHelp and exit script
· there are getopts demos linked in resources, as well as in this folder
3. Create a function called printHelp. printHelp should output the following:
Usage: namechange -f find -r replace "string to modify"
-f The text to find in the filename
-r The replacement text for the new filename
4. If no filename was provided OR if filename does not exist:
· Output User must provide valid filename
· Call the printHelp function
5. Using argument in field filename, find the pattern to be replaced and replace it with the pattern requested using sed
· Hint: you may just want to have sed use the -E option
6. Rename the file.
# Sample runs of working script
$ bash namechange -h
Usage: namechange -f find -r replace filename
-f The text to find in the filename
-r The replacement text for the new filename
$ bash namechange -f "\s" -r "-" "hello world.md"
Renamed "hello world.md" to hello-world.md
$ bash namechange -f "er+" -r "error" spellingerrrr.txt
Renamed spellingerrrr.txt to spellingerror.txt
· Resources
· bash-hackers - getopts tutorial
· assertnotmagic - breaking down how getopts works
· sed with string, not input file
- Bulk Renamer
This builds on the script created for Part 1. Since the core is similar, I would cp this to a new script named bulkrenamer.
1. Download and run one of the create files scripts to generate some dummy files for this part.
· createfiles.sh will just create 30 files in the directory you run it from (10 of each error type below)
· cfargs.sh will let you create a number of files with the error type of your choice (spaces or spelling) in the directory you specify
· cfgetopts.sh will let you do the same as cfargs.sh, but with options
· Errors in these file names that can be corrected with a script:
· jpg is misspelled as jgp
· spaces in file names that could be replaced with -
· files with foo in them need to become bar
2. The script will run as: bulkrenamer -f find -r replace FILES_TO_RENAME*
3. For each file given (or all files in a given folder), rename according to the find / replace arguments provided.
· Resources