LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2008, 04:53 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 426

Rep: Reputation: 31
bash how to pass value to variable in this function?


i use this function to delete contents of directory
with too much stuff for bash.

Code:
cd /home/xfers/directory_with_lots_of_files

function delete_all ()
     {
          while read line1; do

            echo $line1
            rm /home/xfers/directory_with_lots_of_files/$line1

          done
     }

ls -l /home/xfers/directory_with_lots_of_files | delete_all
i want to use it to pass the filenames of the first
and last files now in this directory to two variables.
find errors out as there are many thousands of files
in the directory. this is what i've changed it to for
starters:

Code:
cd /home/xfers/directory_with_lots_of_files

function find_first ()
     {
          while read line1; do

            echo $line1

          done
     }

ls -l /home/xfers/directory_with_lots_of_files | find_first
this does fine at printing a directory listing and
i can change the final line to 'ls -rl' to do a
reverse listing, but i can't seem to retain the
first or last value as a variable. how would i pass
the value of the last file listed by this function
using either the 'ls -l' or 'ls -rl' form? knowing
those, i could pass the first and last filenames in
this large directory on for further processing but
i seem to be stuck.

thanks,
BabaG

Last edited by babag; 04-02-2008 at 05:00 PM.
 
Old 04-02-2008, 06:36 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,442

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Don't understand why you want 'first' & 'last' filenames.
There are 2 options I'd use to 'empty' a large dir.

Code:
# option 1 delete dir & replace it
rm -rf $targetdir
mkdir $targetdir

# option 2: (slower) use a simple loop
cd $targetdir
for file in `ls`
do
    rm $file
done
HTH
 
Old 04-02-2008, 08:21 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 426

Original Poster
Rep: Reputation: 31
thanks chris.

i'm just using the delete function as a starting point
to build a larger function for another purpose. the
'delete_all' function is working fine as it is for
emptying my large directory. the other function i have
to create is to split the directory's contents into
six equal (roughly), parts and copy each of the six
parts to a different directory.

my main directory has many thousands of numbered files.
the filenames are in a convention of:

name_of_project-01234.tga

i need to find the total number of files, divide it by
six. i'll then find the first file in the directory,
find its number (the 01234 part) and add number_of_files/6
to it. that will give me the first and last file for part
one of six. i can then do something like that for the next
five parts. that should give me an articulation of which
files belong to which of the six parts. then i can begin
copying the six parts to the six directories.

i just need to get the first and last filenames in the
directory into a variable so i can begin to do the simple
math and filename parsing. i can't use something like
the 'find' command because there are too many files in the
directory for bash to process without an error. that's why
i'm using my 'delete_all' function as a starting point. it
avoids the bash error.

but how do i pass a variable value for the first and last
filenames from within the function so that i can do my
parsing and math?

thanks again,
BabaG

Last edited by babag; 04-02-2008 at 08:23 PM.
 
Old 04-02-2008, 08:41 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,417
Blog Entries: 55

Rep: Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627Reputation: 3627
I'm not convinced the amount of files is the problem. For example if I build a BaSH array like: 'array=($(find /home/xfers/directory_with_lots_of_files -type f))'it holds 130K worth of filenames easily. I loathe using 'ls' but if it really is too much, and if sort criterium can be time then something like 'ls -t|head -1' and 'ls -tr|head -1' could work?
 
Old 04-02-2008, 08:42 PM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you can use sed, tail, head or a for loop there but since you want to use ls, you can do
Code:
FIRSTFILE=$(ls -1 | while read LINE; do echo "${LINE}"; break; done)
LASTFILE=$(ls -1r | while read LINE; do echo "${LINE}"; break; done)
note that you can't directly do
Code:
ls -1 | while read FIRSTFILE; do break; done
you'll only have the variable saved in the subprocess

Last edited by konsolebox; 04-02-2008 at 08:47 PM. Reason: that should be 'ls -1r' and 'ls -1'
 
Old 04-02-2008, 08:52 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
.. but i still don't get it .. do you mean something like this:
Code:
ls -1 | {
	read FIRSTFILE
	# process first file

	while read FILE; do
		# process second to last file
		LASTFILE=${FILE}
	done
}
or
Code:
process() {
	read FIRSTFILE
	# process first file	

	while read FILE; do
		# process second to last file
		LASTFILE=${FILE}
	done
}
ls -1 | process
?

Last edited by konsolebox; 04-02-2008 at 08:58 PM. Reason: that should be while not until, LASTFILE was blank
 
Old 04-02-2008, 09:27 PM   #7
babag
Member
 
Registered: Aug 2003
Posts: 426

Original Poster
Rep: Reputation: 31
unSpawn:

the directory has something like 25000 files all with
long filenames. each file is about 1 megabyte. whenever i try
using simple bash commands i get an error. i read somewhere about
bash limitations and that i should try using functions in this kind
of circumstance. that has made it possible to do things like my 'delete_all' function. 'rm' errored in this instance but the 'delete_all' function works so i can only assume the limitations i read about are
coming into play here.

konsolebox:

tried the first suggestion:
Code:
FIRSTFILE=$(ls -1 | while read LINE; do echo "${LINE}"; break; done)
LASTFILE=$(ls -1r | while read LINE; do echo "${LINE}"; break; done)
but it just sat for a while and then returned to the command prompt.

added an 'echo $FIRSTFILE' and 'echo $LASTFILE' at the end and that
returned:

total 23500705
total 23500705

still no luck in passing the first and last filenames in this large
directory to variables.

thanks again,
BabaG
 
Old 04-02-2008, 11:02 PM   #8
babag
Member
 
Registered: Aug 2003
Posts: 426

Original Poster
Rep: Reputation: 31
this is seeming to be a scope issue. i'm using mandriva
2007.1. i'm finding that variables defined inside the
function are lost when the function completes. variables
defined outside the function are taken into the function
and manipulated, but revert to their original,
outside-the-function, value when the function finishes.

this is contrary to what i've seen online where i've looked
at more than one n00b example page that says the behaviour
i'm seeing should be the reverse, that, unless declared as
'local' within the function, the variables should carry over
as global variables.

thanks,
BabaG
 
Old 04-02-2008, 11:07 PM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
use number 1 not el: 'ls -1' and 'ls -1r'
 
Old 04-02-2008, 11:18 PM   #10
babag
Member
 
Registered: Aug 2003
Posts: 426

Original Poster
Rep: Reputation: 31
DOH!

that did it. thanks konsolebox!

BabaG
 
Old 04-02-2008, 11:42 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you're welcome.. btw .. this should make things easier:
Code:
FIRSTFILE=$(ls -1 | ( read LINE; echo "${LINE}"; ))
LASTFILE=$(ls -1r | ( read LINE; echo "${LINE}"; ))
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to pass a c variable to a bash script? daYz Programming 3 09-28-2007 08:30 AM
Pass a variable as an agruement for cut chipmanchu Programming 1 05-25-2005 11:39 AM
Bash Script Passing variable to Function nutthick Programming 2 02-02-2005 05:15 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM
Pass text to variable Zed Linux - Software 6 05-12-2003 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 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