Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

a way to increment a value in a function?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
b4w
Posts: 21
Joined: 2015-08-04 19:06

a way to increment a value in a function?

#1 Post by b4w »

Is there a way to increment a value in a function?
bash file has 12 lines like this.
espeak -f alice/text/01.txt -w alice/wav/01.wav;
espeak -f alice/text/12.txt -w alice/wav/12.wav;
Note that each line differs only by an incremented digit, occuring twice in each line.
Can the number of lines be decreased?

Sorry, The extension txt is not allowed for attachment. So here is the bash file.
echo "bash alice/bash.txt";
# pgdvd042010/1/11/11.ZIP
unzip alice/11.ZIP -d alice/;
# rpl "*" " " alice/11.txt;

# mkdir alice/text/;
# csplit alice/11.txt -falice/text/ -b%.2d'.txt' "/CHAPTER/" {11} "/End of/";

# mkdir alice/wav/;
espeak -f alice/text/01.txt -w alice/wav/01.wav;
espeak -f alice/text/02.txt -w alice/wav/02.wav;
espeak -f alice/text/03.txt -w alice/wav/03.wav;
espeak -f alice/text/04.txt -w alice/wav/04.wav;
espeak -f alice/text/05.txt -w alice/wav/05.wav;
espeak -f alice/text/06.txt -w alice/wav/06.wav;
espeak -f alice/text/07.txt -w alice/wav/07.wav;
espeak -f alice/text/08.txt -w alice/wav/08.wav;
espeak -f alice/text/09.txt -w alice/wav/09.wav;
espeak -f alice/text/10.txt -w alice/wav/10.wav;
espeak -f alice/text/11.txt -w alice/wav/11.wav;
espeak -f alice/text/12.txt -w alice/wav/12.wav;

# mkdir alice/ogg/;
sox alice/wav/01.wav alice/ogg/01.ogg;
sox alice/wav/02.wav alice/ogg/02.ogg;
sox alice/wav/03.wav alice/ogg/03.ogg;
sox alice/wav/04.wav alice/ogg/04.ogg;
sox alice/wav/05.wav alice/ogg/05.ogg;
sox alice/wav/06.wav alice/ogg/06.ogg;
sox alice/wav/07.wav alice/ogg/07.ogg;
sox alice/wav/08.wav alice/ogg/08.ogg;
sox alice/wav/09.wav alice/ogg/09.ogg;
sox alice/wav/10.wav alice/ogg/10.ogg;
sox alice/wav/11.wav alice/ogg/11.ogg;
sox alice/wav/12.wav alice/ogg/12.ogg;
echo "end";

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: a way to increment a value in a function?

#2 Post by kiyop »

How about reading http://stackoverflow.com/questions/1695 ... es-in-bash ?

Code: Select all

#!/bin/bash
a=1
while [ $a -le 12 ];do
if [ $a -lt 10 ];then b="0$a";else b=$a;fi

COMMAND_AS_YOU_LIKE_USING_$b

a=$[a+1]
done
ADDED at Wed Oct 14 21:15:42 JST 2015;

The code suggested by Mute Ant in the following post is better than the above code of mine.
Thank Mute Ant for posting better command :)
Last edited by kiyop on 2015-10-14 12:17, edited 2 times in total.
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

Mute Ant
Posts: 9
Joined: 2014-08-10 12:39

Re: a way to increment a value in a function?

#3 Post by Mute Ant »

They look like numbers, but bash stores them as text...

Code: Select all

for a in {101..112} ; do b=${a: -2} && sox alice/wav/$b.wav alice/ogg/$b.ogg ; done

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: a way to increment a value in a function?

#4 Post by HarborView »

I like the C derived code.

Code: Select all

for (( n=1; n<=12; n++ )); do
    a="0"$n
    b=${a:(-2)}  # Grabs the  last 2 characters of a string.
    echo $b
done

HarborView
Posts: 41
Joined: 2014-02-01 02:21
Location: The Space Coast of the USA

Re: a way to increment a value in a function?

#5 Post by HarborView »

After seeing Mute Ant's solution I was inspired to fool around and see what error message I would get. Instead I discovered a sort of left zero function in Bash.

Code: Select all

#!/bin/bash
:<<INTRO
left-zero.sh, dhm, Nov'15
Illustrating an odd left zero fill technique in Bash.
However, if you put a string like 008 or 039 as a number you will get an
error message and possibly an abend.  

If someone knows an "isnumeric" test in Bash, please speak up.
INTRO

for LeftZero in {001..100} ; do

  echo -n $LeftZero

  (( ++k % 10 == 0 )) && echo || echo -en "\t"  # print columns of 10
done
echo

b4w
Posts: 21
Joined: 2015-08-04 19:06

how to break bash for loop on error?

#6 Post by b4w »

Thank you all. My new bash file works good, but could be improved. But to stay on topic, I want to replace 12 with *, run loop until error, run loop until no more data. Or mabye put the 12 into array. mabye use break statement.
for i in {01..12}; espeak -f ${OUT}text/$i.txt -w ${OUT}wav/$i.wav; done
error= Failed to read file 'alice/text/{01..*}.txt'
I tried to upload here, but got this error. "Sorry, the board attachment quota has been reached." So here is my new Alice bash file. I am also working on the bible, which is much more complicated. If you would like to help with that, tell me where to send it.

echo "bash ebook/bash/Alice.txt This Debian bash file converts project gutenberg, from http://www.gutenberg.org/cdproject/pgdv ... so.torrent, pgdvd042010/1/11/11.ZIP Alices Adventures in Wonderland into audio files. 406mb, 5 minutes.";
echo "install required packages. requires root privledge.";
# apt-get install --allow-unauthenticated -y espeak sox;
echo "define path.";
# ${IN} origional zip file location.
IN="/media/r/seagate/a/document/gutenberg/pgdvd042010/1/11/11.ZIP";
# ${OUT} put everything into this location, to be overwriten.
OUT="ebook/alice/";
echo ${IN} ${OUT}; rm -r ${OUT}; mkdir -p ${OUT};
echo "unzip origional file to its default filename."; cp ${IN} ${OUT}; unzip ${OUT}11.ZIP -d ${OUT};
echo "create shadow file to modify."; cp ${OUT}11.txt ${OUT}a.txt;
echo "remove items not appropiate for speech."; rpl "*" " " ${OUT}a.txt;
echo "split book into chapters."; mkdir ${OUT}text/; csplit ${OUT}a.txt -f${OUT}text/ -b%.2d'.txt' "/CHAPTER/" {11} "/End of/";
echo; echo -n "convert txt to wav"; mkdir ${OUT}wav/;
for i in {01..12}; do echo -n " $i"; espeak -f ${OUT}text/$i.txt -w ${OUT}wav/$i.wav; done
echo; echo -n "convert wav to ogg"; mkdir ${OUT}ogg/;
for i in {01..12}; do echo -n " $i"; sox ${OUT}wav/$i.wav ${OUT}ogg/$i.ogg; done
echo "end";
# play ${OUT}ogg/01.ogg;

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: a way to increment a value in a function?

#7 Post by GarryRicketson »

@Unread postby b4w » Please start using

Code: Select all

[code]Code Boxes
[/code]
for your code:

Code: Select all

for i in {01..12}; espeak -f ${OUT}text/$i.txt -w ${OUT}wav/$i.wav; done
error= Failed to read file 'alice/text/{01..*}.txt' 
I tried to upload here, but got this error. "Sorry, the board attachment quota has been reached."
You can not attach files on this forum.Attachments, How to post a screen shot and use code boxes
Thank you.

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: how to break bash for loop on error?

#8 Post by kiyop »

b4w wrote:Thank you all. My new bash file works good, but could be improved. But to stay on topic, I want to replace 12 with *, run loop until error, run loop until no more data. Or mabye put the 12 into array. mabye use break statement.
for i in {01..12}; espeak -f ${OUT}text/$i.txt -w ${OUT}wav/$i.wav; done
error= Failed to read file 'alice/text/{01..*}.txt'
I am confused.
You cannot replace "12" with "*".
"*" has special meanings.
1) All files in the current working directory. You can check by

Code: Select all

ls *
2) O or some itteration of the word before it.

Code: Select all

echo "BAAAAACK"|sed -e "s/A*/A/g"
Maybe

Code: Select all

for i in $(ls ${OUT}text/*.txt);do espeak -f $i -w $(echo $i|sed -e "s/${OUT}text\/\(.*\)\.txt/${OUT}wav\/\1\.wav/"); done
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
kiyop
Posts: 3983
Joined: 2011-05-05 15:16
Location: Where persons without desire to improve themselves fear to tread, in Japan
Been thanked: 3 times

Re: how to break bash for loop on error?

#9 Post by kiyop »

b4w wrote: echo "bash ebook/bash/Alice.txt This Debian bash file converts project gutenberg, from http://www.gutenberg.org/cdproject/pgdv ... so.torrent, pgdvd042010/1/11/11.ZIP Alices Adventures in Wonderland into audio files. 406mb, 5 minutes.";
echo "install required packages. requires root privledge.";
# apt-get install --allow-unauthenticated -y espeak sox;
echo "define path.";
# ${IN} origional zip file location.
IN="/media/r/seagate/a/document/gutenberg/pgdvd042010/1/11/11.ZIP";
# ${OUT} put everything into this location, to be overwriten.
OUT="ebook/alice/";
echo ${IN} ${OUT}; rm -r ${OUT}; mkdir -p ${OUT};
echo "unzip origional file to its default filename."; cp ${IN} ${OUT}; unzip ${OUT}11.ZIP -d ${OUT};
echo "create shadow file to modify."; cp ${OUT}11.txt ${OUT}a.txt;
echo "remove items not appropiate for speech."; rpl "*" " " ${OUT}a.txt;
echo "split book into chapters."; mkdir ${OUT}text/; csplit ${OUT}a.txt -f${OUT}text/ -b%.2d'.txt' "/CHAPTER/" {11} "/End of/";
echo; echo -n "convert txt to wav"; mkdir ${OUT}wav/;
for i in {01..12}; do echo -n " $i"; espeak -f ${OUT}text/$i.txt -w ${OUT}wav/$i.wav; done
echo; echo -n "convert wav to ogg"; mkdir ${OUT}ogg/;
for i in {01..12}; do echo -n " $i"; sox ${OUT}wav/$i.wav ${OUT}ogg/$i.ogg; done
echo "end";
# play ${OUT}ogg/01.ogg;
This is the code (shell script), isn't it?

To deal all the files the name of which end with ".txt" in the directory "text", for example,

Code: Select all

curdir=$PWD
IN="/media/r/seagate/a/document/gutenberg/pgdvd042010/1/11/11.ZIP"
OUT="ebook/alice/"
rm -r $OUT; mkdir -p $OUT
cd $OUT
cp ${IN} ./; unzip 11.ZIP -d ./
cp 11.txt a.txt
rpl "*" " a.txt # I do not know rpl command. This may cause problems.
mkdir text; csplit a.txt -ftext/ -b%.2d'.txt' "/CHAPTER/" {11} "/End of/" # I do not know csplit command. This may cause problems.
mkdir wav
for i in text/*.txt; do echo -n " $i"; espeak -f $i -w wav/$(echo $i|sed -e "s@text/\(.*\)\.txt@\1@").wav; done # I do not know espeak command. This may cause problems.
mkdir ogg
for i in wav/*.wav; do echo -n " $i"; sox $i ogg/$(echo $i|sed -e "s@wav/\(.*\)\.wav@\1@").ogg; done # I do not know sox command. This may cause problems.
cd $curdir
Openbox, JWM: Jessie, Sid, Arch / Win XP (on VirtualBox), 10
http://kiyoandkei.bbs.fc2.com/

User avatar
swirly_cloud
Posts: 69
Joined: 2015-07-08 04:56
Location: Where sun shines.

Re: a way to increment a value in a function?

#10 Post by swirly_cloud »

You can read up on regular expressions. They come in handy for some advanced bash scripting.

You can replace all files that include a change within a specific number interval with "test[0-3]filename". This expression will match test0filename, test1filename, test2filename and test3filename.

Post Reply