LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-27-2004, 12:47 PM   #1
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Rep: Reputation: 15
ksh directory comparison script


I'm pretty new to shell scripting and need to write a script that will add a dash to sets of numbers and take the new numbers(directory names) and compare them with currently existing directories creating a new document. If the directories are there, I need the number and size, otherwise I need the number and it to say closed. So far:

Code:
#!/bin/ksh

sed -e 's/\(...\)\(....\)/\1-\2/' directories.txt

if [ ! -d unk ] ; then
	print closed
else
	du unk > dir.txt
fi
Where I put "unk" means I'm not sure what I should do there. I have been searching and reading and haven't found a simple way to take what the sed command gives me and test that. There's gotta be a better way to do this but I can't think of it atm.
 
Old 08-27-2004, 04:20 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
I have no idea what you're trying to do, but from your code I guess you want this:

Code:
#!/bin/ksh

cd /path/to/somewhere

touch newdirectory.txt

while read dirname 
do
  dirname=`echo $dirname"-"`
  if [  -d  $dirname  ] ; then
	echo "$dirname `du $dirname`" >> newdirectory.txt                
  else
                echo "$dirname not found"
  fi
done <  /path/to/directories.txt

Last edited by jim mcnamara; 08-27-2004 at 04:22 PM.
 
Old 08-30-2004, 06:48 AM   #3
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks for the reply, I'll give it a shot.

Giving it a try. I'm understanding this a little better but I don't think this is what I need. I need to take a txt document that has a listing of directories that may or may not exist and add a "-" taking them from 0000000 to 000-0000(hence the sed command.) I would then need to test the new listing that I'll get from the sed command to see if the directories exist(printing du or closed along with the directory name.) I'd like to be able to take the output of the sed and run it through the if loop.

Last edited by SeT; 08-30-2004 at 07:22 AM.
 
Old 08-30-2004, 09:30 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
I think this will do what you want:

Code:
for rec in `cat t.t`
do
    t1=`echo $rec|cut -c1-3`
    t2=`echo $rec|cut -c4-7`
    t3=${t1}-${t2}
    echo $t3 >>t2.t
done
for rec in `cat t2.t`
do
    if [[ -d $rec ]]
    then
        echo $rec is a dir
    fi
done
where t.t is:
1234567
2345678

you'll get a t2.t:
123-4567
234-5678

which is then tested rec by rec as dirs.
 
Old 08-30-2004, 11:57 AM   #5
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
Ahh, a few slight modifications and it's sorted. Thank you very much.
 
  


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
Can somebody help me with a ksh script? twentymil AIX 7 01-21-2004 09:55 AM
ksh script problem pldobs Programming 2 12-24-2003 11:38 PM
KSH script AquamaN Programming 2 12-08-2003 11:34 AM
current working directory in ksh prompt brian0918 Linux - Newbie 2 06-03-2003 04:34 PM
current working directory in ksh prompt brian0918 Linux - Software 2 06-03-2003 01:50 PM

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

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