The uninterpreted shell arguments are $1
, $2
, etc. You need to put their expansion in double quotes in most contexts, to avoid the value of the parameter being expanded further. "$@"
gives you the list of all parameters.
For example, if you want to pass an argument of the shell script to your function, call it like this:
first_argument_as_filename_in_unix_syntax=$(posix "$1")
The double quotes are necessary. If you write posix $1
, then what you're passing is not the value of the first parameter but the result of performing word splitting and globbing on the value of the first parameter. You will need to use proper quoting when calling the script, too. For example, if you write this in bash:
myscript c:\path with spaces\somefile
then the actual, uninterpreted arguments to myscript
will be c:path
, with
and spacessomefile
. So don't do this.
Your posix
function is wrong, again because it lacks double quotes around $1
. Always put double quotes around variable and command substitutions: "$foo"
, "$(foo)"
. It's easier to remember this rule than the exceptions where you don't actually need the quotes.
echo
does its own processing in some cases, and calling external processes is slow (especially on Windows). You can do the whole processing inside bash.
posix () {
path="${1//\\//}"
case "$path" in
?:*) drive="${p:0:1}"; drive="${drive,}"; p="/$drive/${p:2}";;
esac
printf %s "$p"
}
The zsh feature that jw013 alluded to doesn't do what you seem to think it does. You can put noglob
in front of a command, and zsh does not perform globbing (i.e. filename generation, i.e. expansion of wildcards) on the arguments. For example, in zsh, if you write noglob locate *foo*bar*
, then locate
is called with the argument *foo*bar*
. You'd typically hide the noglob
builtin behind an alias. This feature is irrelevant for what you're trying to do.
I would change a few things about.
find_code() {
# assign all arguments (not just the first ${1}) to MATCH
# so find_code can be used with multiple arguments:
# find_code errorCode
# find_code = 1111
# find_code errorCode = 1111
MATCH="$@"
# For each file that has a match in it (note I use `-l` to get just the file name
# that matches, and not the display of the matching part) I.e we get an output of:
#
# srcdir/matching_file.c
# NOT:
# srcdir/matching_file.c: errorCode = 1111
#
grep -lr "$MATCH" ${SRCDIR} | while read file
do
# echo the filename
echo ${file}
# and grep the match in that file (this time using `-h` to suppress the
# display of the filename that actually matched, and `-n` to display the
# line numbers)
grep -nh -A5 -B5 "$MATCH" "${file}"
done
}
Best Answer
EDIT: Silly me. Of course ^M is CR; and your command should work (works on my system). However, you need to type Ctrl-V Ctrl-M to get the literal '\r'/CR (and not two characters,
^
andM
).Alternatives:
Do this:
Or this:
You can also use the file utility (not sure if it comes with GIT bash):