Simple Korn script

File size and type

Page 1 of 1

14 Replies - 1235 Views - Last Post: 19 November 2009 - 09:15 PM Rate Topic: -----

#1 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Simple Korn script

Post icon  Posted 18 November 2009 - 01:47 AM

I'm supposed to print information about filespace objects. The script is to take a list of file system objects on the command line. For each of those objects, it is to do the following:
• if the object is a regular file, you are to print the name of the file, its size, and the type
of the contents as reported by the file command.
• if the object is a symbolic link, print the name, "symbolic link " and the name of
the linked-to file
• if the object is a directory, do the same things on the objects in the directory (where
the file name is to include the directory name)
• if the object is anything else, print the name and "unknown".

I don't know how to print the size and the type of the file.
I also don't know how to cd into the directory, if there is one to check it's files. Please help

What I have so far is this.
#!/usr/bin/ksh

if [[ ! -e $1 ]]; then
	print "File $1 does not exist."
	return 1
fi  

if [[ -d $1 ]]; then
	print "$1 is a directory."
fi

if [[ -f $1 ]]; then
	print "$1 is a regular file."
elif [[ -L $1 ]]; then
	print "$1 is a symbolic link."
else
	print "$1 is an unknown file type."



Is This A Good Question/Topic? 0
  • +

Replies To: Simple Korn script

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 18 November 2009 - 01:59 AM

View Postebadamageplan, on 18 Nov, 2009 - 02:47 AM, said:

I also don't know how to cd into the directory, if there is one to check it's files. Please help


#!/usr/bin/ksh

if [[ -d $1 ]]; then
	print "$1 is a directory."
	cd $1
	if [[ $? -ne 0 ]]; then
	  print "failed to enter ${1}"
	  exit 1
	fi
	echo ${PWD}
	FILES=`ls`
	for FILE in ${FILES}
	do
	  if [[ -d ${FILE} ]]; then
		print "${FILE} is a directory."
	  fi
	  if [[ -f ${FILE} ]]; then
		print "${FILE} is a file."
	  fi
	done
fi



You can set the output of ls into the variable FILES, then you can loop through FILES with each entry being referenced by FILE.
Was This Post Helpful? 1
  • +
  • -

#3 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 18 November 2009 - 05:33 PM

 FILES=`ls`


So FILES is a variable holding the information that ls returns

for FILE in 

This part I don't get

${FILES}

And this I'm assuming goes through all the files returned.

Is that correct?

And say in the list there is a directory, I go into it, check those files, how do I make sure it returns back out of the directory and continues with the original one?
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 18 November 2009 - 07:23 PM

View Postebadamageplan, on 18 Nov, 2009 - 06:33 PM, said:

 FILES=`ls`


So FILES is a variable holding the information that ls returns

Correct.

View Postebadamageplan, on 18 Nov, 2009 - 06:33 PM, said:

for FILE in 

This part I don't get

It's a for loop. For (variable) in (An array of variables)

Think of it like in c
for(i=0;i<100;i++) { files[i]) }

View Postebadamageplan, on 18 Nov, 2009 - 06:33 PM, said:

And say in the list there is a directory, I go into it, check those files, how do I make sure it returns back out of the directory and continues with the original one?


after you are done in the directory, use cd space .. to drop back one directory.

cd ..
Was This Post Helpful? 1
  • +
  • -

#5 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 18 November 2009 - 11:02 PM

I've updated my code and I've still got some questions

I set a variable START to store the directory the script starts in.
I test it against ${PWD} later because I'm assuming $[PWD} is the current working directory, is this incorrect? Basically I want to know the best way to make sure the script runs, reads each file and directory in the starting directory, if it goes down a level, it should read those files and go into another directory if need be. The biggest concern is going all the way in and coming all the way back out.

I'm worried there's a logic problem with my loop, are there any holes?

Finally, how can I print the size and type of the regular files?

#!/usr/bin/ksh

START = ${PWD}
FILES = 'ls'

for FILE in ${FILES}
do
	if [[ -d ${FILE} ]]; then
		print "${FILE} is a directory."
		cd ${FILE}
	fi

	if [[ -f ${FILE} ]]; then
		print "${FILE} is a regular file."
	elif [[ -L ${FILE} ]]; then
		print "${FILE} is a symbolic link."
	else
		print "${FILE} Is an unknown file type."
	fi

	if [[ ${PWD} -ne START ]] then
		cd ..
	fi
done


Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 18 November 2009 - 11:07 PM

View Postebadamageplan, on 19 Nov, 2009 - 12:02 AM, said:

I'm assuming $[PWD} is the current working directory, is this incorrect?

PWD = Present Working Directory.

I'll take a look over your code, & answer your questions when I get a moment.
Was This Post Helpful? 1
  • +
  • -

#7 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 18 November 2009 - 11:31 PM

One more quick question in my assignment my professor says:

"I want you to turn it in by copying the source file together with a Makefile with a name ending in “.mak” and the executable must be called proj3. Also ensure that the files in the grading directory are readable by other AFTER you turn them in; if I can’t read them, I can’t grade them, and you get 0."

How do I make a makefile that changes the name of my source file if I'm not compiling anything? Also is there a way to make the executable I output from the makefile have set permissions?
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 18 November 2009 - 11:57 PM

uhm... what?

I have never heard of a make file for a korn shell script. Are you sure you are not supposed to be witting this in C or another compiled language?
Was This Post Helpful? 0
  • +
  • -

#9 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 19 November 2009 - 12:02 AM

Here's a link to the assignment

http://www.cs.umsl.e...s2750/proj3.pdf
Was This Post Helpful? 0
  • +
  • -

#10 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 19 November 2009 - 12:06 AM

Quote

The makefile is to copy the file to the name proj3 and make proj3 executable by everyone.


This makes more sense.

You would simply set mode 777 on the file. Check this tutorial for more information about file permissions.

If your make script is named .mak, you can still launch it with sh make.mak or whatever you decide to call it. Sounds like it's just a simply copy, with mode change. Since chmod is a root level command, it would be wise to check for user permissions with whoami.
Was This Post Helpful? 0
  • +
  • -

#11 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 19 November 2009 - 12:31 AM

My professor just runs a script to collect everything and basically it's make -f *mak . Since the script can't originally be called prog3, is this an acceptable makefile for this? Or is there a better way? I know this isn't copying so is there a better way?

prog3 : korn
	mv korn prog3
	chmod 777 prog3



Also I still don't know how to print the file size.
Is the best way to get the size using ls -l with sed?

This post has been edited by ebadamageplan: 19 November 2009 - 01:00 AM

Was This Post Helpful? 0
  • +
  • -

#12 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 19 November 2009 - 10:49 AM

Got it working

#!/usr/bin/ksh

i=1

for i in "$@"
do
	if [[ -d $i ]]; then
		print "$i is a directory.\n"
		$0  $i/*
	elif [[ -f $i ]]; then
		print "$i is a regular file.\n"
		file $i
		du -sh $i
	elif [[ -L $i ]]; then
		print "$i is a symbolic link.\n"
	else
		print "$i Is an unknown file type.\n"
	fi

done



Thanks for all your help

This post has been edited by ebadamageplan: 19 November 2009 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 19 November 2009 - 08:58 PM

Rather than having 3 else ifs, I would use a select case or individual if statements.

Can the Symbolic Link be detected as both a directory & a link?
Was This Post Helpful? 0
  • +
  • -

#14 ebadamageplan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: Simple Korn script

Posted 19 November 2009 - 09:06 PM

What's the advantage of three separate if statements of the case?
Was This Post Helpful? 0
  • +
  • -

#15 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5379
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Simple Korn script

Posted 19 November 2009 - 09:15 PM

I would think that the last else is only going to 'else' the previous else if.

It's mostly just personal preference, but I don't like else if.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1