LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-03-2007, 11:24 AM   #1
Benanzo
Member
 
Registered: Sep 2005
Location: Seattle
Distribution: Ubuntu, Debian flavors
Posts: 119

Rep: Reputation: 15
bash scripting help want to execute external function on current item.


Hello!

I am needing some help with writing a bash script.
I am currently in the process of revising a podcast management script I wrote to add some new functionality. The way I have it set up now is purely with variables and explicitly stating the commands in the body of the script. This is not exactly efficient because I have about 20 individual podcasts to manage. I am trying to split some of the routines out into functions. But instead of just blobbing it all into one long file I want to split the different steps into different scripts that only execute if called. This leads to me needing to run a function in a script on an item in a different script. For instance:

The first part of my script checks the download folders for fresh podcasts and lists them in a file for the rest of the scripts to find them easy:

Code:
#!/bin/bash
#
# Step 1
# Check raw_podcasts for new podcasts

READFILE="/home/ben/podcasts/readfile"

# check for new podcasts
function check_raw_podcasts
{
	for NEW_MP3 in $(find /home/ben/podcasts/raw_podcasts/*/*".mp3") ; do
		echo ${NEW_MP3} >> ${READFILE}
	done

	for NEW_M4A in $(find /home/ben/podcasts/raw_podcasts/*/*".m4a") ; do
		echo ${NEW_M4A} >> ${READFILE}
	done
}
$(check_raw_podcasts)
After the readfile has been populated with new podcasts, step 2 of my script checks the readfile and runs the appropriate functions specific to which podcasts are in the list.

Code:
#!/bin/bash
#
# Step 2
# Check the readfile for new podcasts to work on
READFILE="/home/ben/podcasts/readfile"
BUZZ_DIR="/home/ben/podcasts/raw_podcasts/buzz_out_loud"
ENGADGET_DIR="/home/ben/podcasts/raw_podcasts/engadget"
MACOSKEN_DIR="/home/ben/podcasts/raw_podcasts/macosken"

function check_readfile
{
	for PODCAST in $(cat ${READFILE} | grep "$BUZZ_DIR") ; do
		$(buzz_out_loud)
	done

	for PODCAST in $(cat ${READFILE} | grep "$ENGADGET_DIR") ; do
		$(engadget)
	done

	for PODCAST in $(cat ${READFILE} | grep "$MACOSKEN_DIR") ; do
		$(macosken)
	done
}
$(check_readfile)
I want the functions listed in Step 2 $(buzz_out_loud) $(engadget) etc. to be available from a separate script.

I've been scouring for a little bit and I've noticed that sometimes people put something like

Code:
import ../scripts/functions.sh
or something similar at the beginning of a script. Is that basically what I'm looking for? I'm having a little trouble sorting this out. Thanks for your help!

ben
 
Old 02-03-2007, 01:13 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Yes, however in bash the syntax is:

. /somedir/somescript

(Note the the line starts with period-space). This applies the commands in the external file to the environment of the currently running shell.
 
Old 02-03-2007, 02:33 PM   #3
Benanzo
Member
 
Registered: Sep 2005
Location: Seattle
Distribution: Ubuntu, Debian flavors
Posts: 119

Original Poster
Rep: Reputation: 15
Wonderful, now my next question...

That worked great, thanks!

Also, I have another tidbit that I can't seem to find.

How do I make text appear at the top of a page instead of the bottom like it does with cat?

I want to make an HTML log file with a bash script but I want the newest info to be at the top of the page and not way down at the bottom.

For instance, I have several pre-formatted HTML tables that I want to be able to insert in the body of my HTMl page before the oldest ones, not after.
 
Old 02-03-2007, 02:45 PM   #4
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
If you want to reverse the output of a file, use the 'tac' command instead of 'cat'.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Scripting: Editing external files trek413 Linux - Software 1 11-02-2006 04:11 PM
BASH Scripting Troubles with IF and FUNCTION semaja2 Linux - Software 10 01-26-2006 02:04 AM
Bash Scripting - Using a function as test. ldp Programming 1 01-04-2005 06:31 AM
Mail Function in bash scripting chynna_v Programming 2 09-14-2004 04:36 AM
BASH scripting; su to user, commands after dont execute Zero-0-Effect Programming 4 06-07-2004 11:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration