Brian
Brian

Reputation: 4344

Create text file from bash script

I'm playing around with a simpler way to make animated GIFs with captions using gifify (forked from jclem) using ffmpeg and it's captioning library. I tried adding a variable to my script, looking for the optional argument, but I can't even get it to create the temporary .srt file necessary.

Here's my script creating a .txt as proof of concept:

#!/bin/bash

while getopts "t" opt; do
    case opt in
        t) text=$OPTARG;;
    esac
done

shift $(( OPTIND - 1 ))

subtitles=$1

#If there is text present, do this
if [ -z ${text} ]; then
    #Make an empty txt file
    cat >> /tmp/subs.txt
    text=$subtitles
    append ${text}
fi

I then run it with:

sh text.sh -t "This is my text"

The script runs and will echo out the string of text you put into the shell, but it won't add it to the new file. Any thoughts on what I'm doing wrong?

Upvotes: 0

Views: 7662

Answers (2)

Petr Skocik
Petr Skocik

Reputation: 60058

!/bin/bash

1) You need case $opt.

while getopts "t:" opt; do
    case $opt in
        t) text=$OPTARG;;
    esac
done

shift $(( OPTIND - 1 ))

subtitles=$1

Then,

if [ -z "$text" ]; then #safer and just as long as the curly bracket version
    #Make an empty txt file
    : > /tmp/subs.txt #This is how you create an empty file
    cat /dev/null > /tmp/subs.txt #A longer version of the same thing
    #cat >> /tmp/subs.txt #This APPENDS standard input (STDIN) to /tmp/subs.txt
    text="$subtitles"
    #append ${text} #`append` isn't bash
    echo "$subtitles" > /tmp/subs.txt #the .txt file will contain what's in $subtitles

fi

Edit: @Etan Reisner makes a good point about the quotation marks.

1) You don't need them in text=$subtitles; bash handles this OK 2) You don't need them in your case in echo $subtitles either--echo works OK with multiple arguments, which is what a bare $subtitles expands to--but you'd better off putting them there too, to make it work for cases like:

a='-e hello\nworld'
echo "$a" #Without the qutoes, $a would get expanded and `-e` would get treated as a flag to `echo`

I thinks it's a good practice to quote variables in bash defensively and not rely on quirks like that in the assignment in 1) or echo's not distinguishing between echo hello world and echo "hello world".

Upvotes: 1

dganesh2002
dganesh2002

Reputation: 2210

The question is little unclear but here I believe your basic problem is how to create or append a file. Here is the way to create a new file or append it in a shell script. Hopefully this will help. You can use it the way you want ->

Creating a file ->

cat<<EOF>/tmp/subs.txt
${text}
EOF

OR

echo "${text}" >/tmp/subs.txt

Appending a file (note extra '>') ->

cat<<EOF>>/tmp/subs.txt
${text}
EOF

OR

echo "${text}" >>/tmp/subs.txt

The EOF sometimes doesn't work due to tab or white spaces if you dont keep your text left-aligned.

Also regarding "text=$subtitles"; you cannot do that operation after 'cat' so move it before 'cat' command.

Upvotes: 0

Related Questions