spell/typos+quotes to avoid globbing/wordsplitting#5
spell/typos+quotes to avoid globbing/wordsplitting#5geekcomputers merged 2 commits intomastergeekcomputers/Shell:masterfrom unknown repositoryCopy head branch name to clipboard
Conversation
ghost
left a comment
There was a problem hiding this comment.
Hello. These are just some thoughts I had which might be of use. I hope they at least gives you a different perspective.
batch_file_rename.sh
Outdated
| funct_check_params() # Start of the procedure | ||
| { | ||
| if [ ${NARG} -lt 3 ]; then # Check there are 3 arguments passed to the script, if not display the help | ||
| if [ "${NARG}" -lt 3 ]; then # Check there are 3 arguments passed to the script, if not display the help |
There was a problem hiding this comment.
This change shouldn't be needed, because the variable should be an integer. Rather than forcing this within the test, I would make the script more robust by first ensuring it's an integer and catching when it's not.
batch_file_rename.sh
Outdated
| # This will carry out the rename | ||
|
|
||
| for file in $(ls $work_dir/*$old_ext); do mv $file `echo $file | sed 's/\(.*\.\)'$old_ext_cut'/\1'$new_ext_cut/` ; done | ||
| for file in $(ls "$work_dir"/*"$old_ext"); do mv "$file" $(echo "$file" | sed 's/\(.*\.\)'"$old_ext_cut"'/\1'"$new_ext_cut"/) ; done |
There was a problem hiding this comment.
It's almost never recommended or necessary to use ls(1) in a shell script, largely because glob filename pattern matching more than suffices, is more reliable, and is more efficient. In this case, you should be able to:
for file in "$work_dir"/*"$old_ext"; do ...Confirm first, in-case there's something unusual I'm overlooking.
ghost
left a comment
There was a problem hiding this comment.
legacy backticks for $(...) notation
|
I think it's important to note that the use of backticks or graves for command substitution is not going anywhere any time soon, as far as I know. It may be syntax from days of old, but I believe it still holds relevance. Variable=`some_command`In the above example, there is no need for the However, Variable=$(some_command "string $(other_command)")In the above example, using the newer syntax makes sense, because it avoids the confusion between the final double-quote and the right-most grave, and nesting is required. This is just my stance on command substitution syntax in shell programming. I use both, where appropriate. Actually, there is another important argument for the newer method: many claim that the newer syntax is simply easier to see, regardless of what's next to it. However, this largely seems dependent on the user's font. I believe each user should always have a font suitable for such text, so that each character is sufficiently distinguishable from the rest. |
fixing some spelling errors and adding double quotes to avoid globbing or word splitting