Skip to main content
Code Review

Return to Question

the update belongs in a new question
Source Link
russau
  • 223
  • 1
  • 5

I'm pretty new to bash scripting. I want a script that I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi

Update I'd also like to take an optional template parameter. I can do something like this:

if [ $# -gt 0 ]; then
 pandoc $file --template=1ドル -o "../html/${file%md}html"
else
 pandoc $file -o "../html/${file%md}html"
fi

Is there nicer was to optionally deliver the parameter to pandoc?

I'm pretty new to bash scripting. I want a script that I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi

Update I'd also like to take an optional template parameter. I can do something like this:

if [ $# -gt 0 ]; then
 pandoc $file --template=1ドル -o "../html/${file%md}html"
else
 pandoc $file -o "../html/${file%md}html"
fi

Is there nicer was to optionally deliver the parameter to pandoc?

I'm pretty new to bash scripting. I want a script that I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi
Tweeted twitter.com/StackCodeReview/status/1063990191365472256
cleaner title and add beginner tag
Source Link
Stephen Rauch
  • 4.3k
  • 12
  • 24
  • 36

iterate Iterate over files and convert with a new name / directory

I'm pretty new to bash scripting. I want a script athat I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi

Update I'd also like to take an optional template parameter. I can do something like this:

if [ $# -gt 0 ]; then
 pandoc $file --template=1ドル -o "../html/${file%md}html"
else
 pandoc $file -o "../html/${file%md}html"
fi

Is there nicer was to optionally deliver the parameter to pandoc?

iterate over files and convert with a new name / directory

I'm pretty new to bash scripting. I want a script a I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi

Update I'd also like to take an optional template parameter. I can do something like this:

if [ $# -gt 0 ]; then
 pandoc $file --template=1ドル -o "../html/${file%md}html"
else
 pandoc $file -o "../html/${file%md}html"
fi

Is there nicer was to optionally deliver the parameter to pandoc?

Iterate over files and convert with a new name / directory

I'm pretty new to bash scripting. I want a script that I can run in a directory that contains .md files, runs pandoc on them, and delivers the output to a sibling folder html, i.e. my folder structure is like this:

.
├── html
└── md

Here is what I have working, I just don't know if I'm following good practices or if there's a nicer / more readable way of doing what I'm doing.

#! /bin/bash
# run this from a folder containing markdown files
# it delivers html to the sibling html folder
if hash pandoc 2>/dev/null; then
 for file in ./*.md ; do
 echo Converting $file
 cat "$file" | pandoc -o "$(echo $file | sed 's/^./..\/html/' | sed 's/.md$/.html/')"
 done
else
 echo "I need pandoc installed"
fi

Update I'd also like to take an optional template parameter. I can do something like this:

if [ $# -gt 0 ]; then
 pandoc $file --template=1ドル -o "../html/${file%md}html"
else
 pandoc $file -o "../html/${file%md}html"
fi

Is there nicer was to optionally deliver the parameter to pandoc?

bash iterate over files and convert with a new name / directory

added 325 characters in body
Source Link
russau
  • 223
  • 1
  • 5
Loading
Source Link
russau
  • 223
  • 1
  • 5
Loading
lang-bash

AltStyle によって変換されたページ (->オリジナル) /