Skip to main content
Commonmark migration
Source Link

#Modified code #!/bin/sh

Modified code

#!/bin/sh

# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES-} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;

#Modified one-liner find . -name *.sh -exec printf '## File: %s\n\n' {} ; -exec sed 's/^/ /' {} ;

Modified one-liner

find . -name \*.sh -exec printf '## File: %s\n\n' {} \; -exec sed 's/^/    /' {} \;

#Modified code #!/bin/sh

# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES-} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;

#Modified one-liner find . -name *.sh -exec printf '## File: %s\n\n' {} ; -exec sed 's/^/ /' {} ;

Modified code

#!/bin/sh

# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES-} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;

Modified one-liner

find . -name \*.sh -exec printf '## File: %s\n\n' {} \; -exec sed 's/^/    /' {} \;
deleted 11 characters in body
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

It's implementation-definedunspecified whether find expands {} when it's a substring of an argument; the safer POSIX way is to pass it as a positional argument and have the child shell expand it:

It's implementation-defined whether find expands {} when it's a substring of an argument; the safer POSIX way is to pass it as a positional argument and have the child shell expand it:

It's unspecified whether find expands {} when it's a substring of an argument; the safer POSIX way is to pass it as a positional argument and have the child shell expand it:

$PRUNE_RULES could be used unset
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES-} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;

#Modified one-liner find . -name *.sh -exec printf '## File: %s\n\n' {} ; -exec sed 's/^/ /' {} ;

# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;
# user-modifiable parameters
FILE_HEADING='## File: %s\n\n'
DEFAULT_IGNORED_DIRS='build .git'
DEFAULT_EXTENSIONS='c cc cpp h hpp'

# end of user variables; code starts here

set -eu  # Usual safety
set -f   # Don't expand pathnames when expanding $NAME_RULES below

for dir in ${IGNORED_DIRS-$DEFAULT_IGNORED_DIRS}; do
    PRUNE_RULES="${PRUNE_RULES-} -name ${dir} -prune -o"  
done

for ext in ${EXTENSIONS-$DEFAULT_EXTENSIONS}; do
    NAME_RULES="${NAME_RULES:--false} -o -name *.${ext}"
done

# We intend word splitting of rules variables
# shellcheck disable=SC2086
exec find -L . ${PRUNE_RULES-} ${NAME_RULES:+'(' $NAME_RULES ')'} -type f \
     -exec printf "${FILE_HEADING}" {} \; \
     -exec sed 's/^/    /' {} \;

#Modified one-liner find . -name *.sh -exec printf '## File: %s\n\n' {} ; -exec sed 's/^/ /' {} ;

Improved full code
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
deleted 32 characters in body; added 62 characters in body; added 29 characters in body
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
Debugged and updated
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
typo
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
added 180 characters in body
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
Separate the printf and sed commands into two predicates
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327
Loading