ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
what this does is find the first file of type .tga with a given prefix to
the filename ($variable) in a directory. that file is assigned to $tmp. i'm using it
to find the first file of a group of numbered files that all have a common prefix.
once found, i can process that file and increment the number to process the rest
of the files sequentially.
i'd like to move this script to linux and so will need to adapt it to bash i suppose.
It's easier to let the shell handle it, rather than trying to increment the count yourself. Zsh can sort files numerically, but bash can't. However, ls can do so with the -v option. E.g.
Code:
#!/bin/sh
for file in $(ls -v $1*.tga); do
# process file
done
and call the script with the prefix name. I'm assuming all the files are in the current directory. If not, just wangle a directory in there, e.g.
Code:
...
dir=$1
prefix=$2
for file in $(ls -v $dir/$prefix*.tgz); do
....
Or with zsh (no need to invoke ls):
Code:
#!/bin/zsh
setopt numericglobsort
for file in $1*.tga; do # zsh sorts files automatically
# process file
done
thanks so much dawkcid. will look into these as soon as i'm back in front of
my linux box.
i was incrementing the files rather than doing a sort each time because i found,
under powershell at least, that the sort was too slow. these are directories of
timelapse still image sequences and have up to 32000 files in them. that being
the case, i found that sorting once to find the first file and then using an
increment routine was much faster than repeated sorts.
just thought of this. powershell allows me to find the last file
in the series just by changing the word 'first' to 'last' in the
example in my original post. how would i do this in a bash
script?
As opposed to most scripting languages like Python and Perl (and even zsh), which substitute the entire array when you do $array, bash only substitutes the first element. To get the entire array you need the slightly clumsy ${array[*]}. The # gives you the length of the variable. Since bash arrays count from zero (zsh on the other hand counts form 1), the last element is one less. The surrounding $(( .. )) performs arithmetic substition.
Once you start doing more scripting, you will probably want to read up on some scripting articles. The standard guide seems to be the Advanced Bash Scripting guide. Don't worry about the "Advanced", it starts off with the basics (apparently, I haven't read it myself). (That link is for the online version, if you google around you can find a tarball of the article that you can read offline.)
Alternatively (should have thought of this yesterday), you could do something like
thanks again dawkcid. the above cmd just paused for a
few seconds then came back to the cmd line. no results.
i'm a perma noob at this stuff so i'm probably missing
something obvious. should i be doing something with grep
to get a result to display?
The ./ is part of the pathname generated by find (meaning the current directory). You can get rid of it with basename or using bash's parameter expansion syntax (which will be more efficient than spawning basename):
Code:
filename=$(basename $filename)
or
Code:
filename=${filename##*/}
You could work basename into the find pipeline, such as
filename=$(basename $(find ... | sort ... | head ...))
Using other way you'd need to do it afterwards since bash cannot do nested parameter expansion. Thus,
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.