# Create content from a bash script

**URL:** <https://discourse.gohugo.io/t/create-content-from-a-bash-script/32550>\
**Category:** tips & tricks\
**Created:** [April 26, 2021, 5:36pm UTC](https://discourse.gohugo.io/t/create-content-from-a-bash-script/32550 "2021-04-26T17:36:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [April 26, 2021, 5:36pm UTC](https://discourse.gohugo.io/t/create-content-from-a-bash-script/32550/1 "2021-04-26T17:36:38Z")

</div>

I noticed @cblte commenting on a post that is 3 years old now, and I went to take a look at how much my script to generate content changed since then.

Here are the useful bits in case someone needs a similar script:

```bash
#!/bin/bash
EDITOR=code
GREEN=$(tput -T screen setaf 2)
POWDER_BLUE=$(tput -T screen setaf 153)
NORMAL=$(tput sgr0)

if [[$1 = "story"]]; then
	#statements
	## Usage
	# `new STORY TITLE-with-dashes`
	# `new story section section-name`

	if [[$2 = 'section']]; then
		hugo new story/"$3"/_index.md
		open content/story/"$3"/
		else
			export STORY=$2
			export SLUG=$3
			DATE=`date "+%Y-%m-%d"`

			hugo new story/$STORY/$DATE-$SLUG/index.md

			export DESTINATION="content/story/$STORY/$DATE-$SLUG"

			mkdir -p $DESTINATION/gallery;
			mkdir -p $DESTINATION/images/;

			open $DESTINATION/; $EDITOR $DESTINATION/; cd $DESTINATION/;

			printf "\n$DESTINATION\n";
	fi

fi

if [[$1 = "post"]]; then
	#statements
	# POSTS
	export SLUG=$2
	DATE=`date "+%Y-%m-%d"`

	hugo new post/$DATE-$SLUG/index.md

	mkdir -p content/post/$DATE-$SLUG/gallery;
	mkdir -p content/post/$DATE-$SLUG/images/;

	open content/post/$DATE-$SLUG/; $EDITOR content/post/$DATE-$SLUG/; cd content/post/$DATE-$SLUG/;
fi

# This was a special project that needed a new page every day
# Usage: `./new pagina-tantas`
if [[$1 = "paginas-tantas"]]; then
	printf "\n ${POWDER_BLUE}### Creating files ###\n${NORMAL}"
	FILE_EN=`date +%Y-%m-%d`
	FILE_PT=`date +%Y-%m-%d`

	hugo new stories/paginas-tantas/$FILE_EN/index.md;
	hugo new stories/paginas-tantas/$FILE_PT/index.pt.md;

	sed -i'' -e 's/draft\: true/draft\: false/g' content/story/paginas-tantas/$FILE_EN/index.md;
	sed -i'' -e 's/draft\: true/draft\: false/g' content/story/paginas-tantas/$FILE_PT/index.pt.md;

	sed -i'' -e 's/stories\:/stories\: \[\"paginas-tantas\"\]/g' content/story/paginas-tantas/$FILE_EN/index.md;
	sed -i'' -e 's/stories\:/stories\: \[\"paginas-tantas\"\]/g' content/story/paginas-tantas/$FILE_PT/index.pt.md;

	rm content/story/paginas-tantas/$FILE_EN/index.md-e
	rm content/story/paginas-tantas/$FILE_PT-e/index.pt.md-e

	printf "\n ${POWDER_BLUE}### Opening files ###\n${NORMAL}"
	$EDITOR content/story/paginas-tantas/$FILE_PT/;
	open content/story/paginas-tantas/$FILE_EN/index.md;
fi

# I think Hugo now does this on it's own.
if [[$1 = 'undraft']]; then
	DATE=`date "+%FT%TZ"`

	gsed -i "s/date:.*$/date: ${DATE}/g" $2;
	gsed -i "/draft: true/d" $2;
fi

# Send it to github
if [[$1 = 'publish']]; then
	if [[$2]]; then
		commit_message="$2"
	else
		commit_message="novo post"
	fi
	git add --all ; git commit -am "$commit_message" ; git push;
fi

if [[$1 = 'deploy']]; then

	printf "\n${GREEN}### Pushing changes and building on the other side ###\n${NORMAL}"
	git push && ssh -t DeLorean 'cd Digital-Insanity; ./build.sh'
fi

if [[$1 = 'help']]; then
	#statements
	echo "${GREEN}post : ${NORMAL}creates a new post, sintax is :: new.sh post TITLE-with-dashes"
	echo "${GREEN}story : ${NORMAL}creates a new post inside a story, sintax is :: new.sh story STORY-NAME TITLE-with-dashes"
	echo "${GREEN}paginas-tantas : ${NORMAL}creates a new post insite the paginas-tantas story"
	echo "${GREEN}publish : ${NORMAL}adds all files to git and commits the changes to the current branch"
	echo "${GREEN}deploy : ${NORMAL}pushes any commits to origin and connects to the DeLorean server to run the build script"
fi

```

In archetypes, if I want to get the title from an env variable, I make sure to export it  
`export SLUG=$3`  
And to fetch it replacing the dashes:  
`title: "{{ replace (getenv "SLUG") "-" " " | title }}"`

Hope this helps someone.

---

<div class="post-metadata">

**Author:** ![cblte](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/cblte/32/14200_2.png) [@cblte](https://discourse.gohugo.io/u/cblte)\
**Post date:** [April 26, 2021, 6:07pm UTC](https://discourse.gohugo.io/t/create-content-from-a-bash-script/32550/2 "2021-04-26T18:07:38Z")

</div>

Thank you even more sharing a new version of the script.  
I actually did not recognize how old the post was, as I was searching in all for “bash script”.  
I modified it a litle bit to my needs, as I want my posts in a folder ‘blog’ with a subfolder of ‘year-month’.

So my part of your ‘post’ block looks now like this:

```bash
    if [[$1 = "blog"]]; then
	#statements
	# blog
	export SLUG=$2
	DATE=`date "+%Y-%m"`

	hugo new blog/$DATE/$SLUG/index.md

	# mkdir -p content/post/$DATE-$SLUG/gallery; # dont need a gallery
	mkdir -p content/blog/$DATE/$SLUG/images/;

	$EDITOR content/blog/$DATE/$SLUG/; cd content/blog/$DATE/$SLUG/;
    fi

```

All I actually need is a little helper to create a new page bundle for a blog post. Currently, migrating a lot of content from my Ghost blog to Hugo.

Oh I should mention, I am using `gitbash` on windows

---

<div class="post-metadata">

**Author:** ![brunoamaral](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/brunoamaral/32/21144_2.png) [@brunoamaral](https://discourse.gohugo.io/u/brunoamaral)\
**Post date:** [April 26, 2021, 6:15pm UTC](https://discourse.gohugo.io/t/create-content-from-a-bash-script/32550/3 "2021-04-26T18:15:27Z")

</div>

That first bit for creating new content is mostly the same, I just kept adding “modules” as I needed them. 🙂

Lately I have been using python to make the whole thing easier to read. Then I just run hugo with this:

```python
args = ("/usr/local/bin/hugo", "-d", website_path,"--cacheDir", path)
popen = subprocess.Popen(args, stdout=subprocess.PIPE, universal_newlines=True)

```
