LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2012, 01:40 PM   #1
betsy1181
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
WHILE loop in Korn Shell


I would like to write a loop that identifies how many number of files in a directory and then does a series of things for the names of the files in the directory. I am struggling to first create an array with the names of the files in the directory. (note: there are three files that begin with srw_. Here is my code so far:

myvar=$(ls /dept/ftp/incoming/srw_* 2> /dev/null | wc -l)
echo $myvar

if [ $myvar -gt 0 ]
then

while((i<$myvar))
do
((i=i+1))
echo $i
myvar2=$(ls /dept/ftp/incoming/srw_*)
done
i=0

I would like myvar2 to store the file names srw_*. Where the star represents the three file names. I know this is confusing which is why I need some help getting started. Thank you in advance for your help!
 
Old 03-01-2012, 10:15 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

you seem to be going about this in a rather awkward way. Have you considered something like the following?
Code:
for f in /dept/ftp/incoming/srw_* ; do
  echo "The file name is $f"
done
HTH,

Evo2.
 
Old 03-02-2012, 12:22 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Its not entirely clear, but
Code:
myvar2=$(ls /dept/ftp/incoming/srw_*)
 
Old 03-02-2012, 02:33 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by betsy1181 View Post
myvar=$(ls /dept/ftp/incoming/srw_* 2> /dev/null | wc -l)
... counts words so any files with word separators (space, tab and newline) in their names will be counted more than once.
 
Old 03-02-2012, 09:23 AM   #5
betsy1181
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thank you! I like the for loop rather than the while loop. I need to do two things with this file extension. I would one like to strip off the bit after the underscore and before the .zip and mkdir directory with file3.zip. I would then like to strip off just the bit before the _ /dept/ftp/incoming/srw_file3.zip and then run an unzip command on the file. Let me show you what I would like to do for this specific case although I will eventually need to do this for all the zip files in that directory.

for f in /dept/ftp/incoming/srw_* ; do
echo "The file name is $f"
mkdir file3
cd file3
unzip srw_file3.zip
done

Of course, file3 will be lots of different file names, but it will always follow after the srw_ and precede the .zip
 
Old 03-04-2012, 06:32 PM   #6
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

sounds like you need to learn about string substitutions/operations in ksh. The following appears to be a good resource.

http://docstore.mik.ua/orelly/unix/ksh/ch04_03.htm

Section 4.3.3 in particular.

Evo2.
 
  


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
learning korn shell Madison00 Linux - Newbie 2 12-15-2010 10:46 AM
korn shell loop jonlake Programming 2 03-28-2008 01:10 PM
Korn Shell PS1 c19h28O2 Linux - Newbie 4 04-25-2006 07:56 AM
Arrays in Korn Shell...? vous Programming 1 03-26-2005 02:35 AM
a t&t korn shell 93 shook Linux - Software 4 02-01-2003 02:54 PM


All times are GMT -5. The time now is 08:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration