#2 Needs more quoting for filenames with spaces

Closed
opened 6 years ago by mulle-nat · 5 comments

To be able to deal with filenames with spaces, here is a better older_than function

# noisily signal success if $1 is older than _any_ of the remaining args
older_than() {
  local target="$1"
  shift
  if [ ! -e "$target" ]
  then
    echo "updating $target" >&2
    return 0  # success
  fi
  local f

  for f in $@
  do
    if [ "$f" -nt "$target" ]
    then
      echo "updating $target" >&2
      return 0  # success
    fi
  done
  return 1  # failure
}
To be able to deal with filenames with spaces, here is a better older_than function ``` # noisily signal success if $1 is older than _any_ of the remaining args older_than() { local target="$1" shift if [ ! -e "$target" ] then echo "updating $target" >&2 return 0 # success fi local f for f in $@ do if [ "$f" -nt "$target" ] then echo "updating $target" >&2 return 0 # success fi done return 1 # failure } ```
akkartik commented 6 years ago
Owner

Thanks!

I think I see one further issue in your version:

< for f in $@
> for f in "$@"

Does this seem right? Reading http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html, it looks like "$@" isn't just a bash-ism.

Thanks! I think I see one further issue in your version: ```diff < for f in $@ > for f in "$@" ``` Does this seem right? Reading http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html, it looks like `"$@"` isn't just a bash-ism.
mulle-nat commented 6 years ago
Poster

Yes that's the proper way to do it as you can test for yourself. "$@" is special..

#! /bin/sh


a()
{
   echo "::a::"
   for i in $*
   do
     echo "$i"
   done
}


b()
{
   echo "::b::"
   for i in $@
   do
     echo "$i"
   done
}


c()
{
   echo "::c::"
   for i in "$@"
   do
     echo "$i"
   done
}

a "1 2" "3"
b "1 2" "3"
c "1 2" "3"

Yes that's the proper way to do it as you can test for yourself. "$@" is special.. ``` #! /bin/sh a() { echo "::a::" for i in $* do echo "$i" done } b() { echo "::b::" for i in $@ do echo "$i" done } c() { echo "::c::" for i in "$@" do echo "$i" done } a "1 2" "3" b "1 2" "3" c "1 2" "3" ```
akkartik commented 6 years ago
Owner

My question was about whether it works on platforms I don't have access to. Thanks! I'll send out a fix.

My question was about whether it works on platforms I don't have access to. Thanks! I'll send out a fix.
mulle-nat commented 6 years ago
Poster

Yes that's bourne shell compatible everywhere.

Yes that's bourne shell compatible everywhere.
akkartik commented 6 years ago
Owner

Committed as 6241e9a4e5. Many thanks!

Committed as 6241e9a4e5c25cc9da82d94fc56b35c17f0b4480. Many thanks!
Sign in to join this conversation.
No Label
No Milestone
No assignee
2 Participants
Loading...
Cancel
Save
There is no content yet.