LinuxQuestions.org
Review your favorite Linux distribution.
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 11-29-2004, 03:31 PM   #1
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Rep: Reputation: 15
Help with command line options(ksh)


Got a script written that will search for files owned by a user. It mostly works but I'm having trouble with command line options. I need it to look for -u(user), -g(calculate total in gb), and -p(specify directory.)
The way it currently runs, I just want to be able to type the script name and have it run as user logged on for all project directories - this much works. When I try to use an option, however, it seems to not work and skip doing anything(I get the final output very fast and notice it ran for user currently logged in. I've also noticed a paste command usage line in the output but the same exact command works outside of the script. Can someone look this over and see where I'm going wrong?

Code:
#!/bin/ksh

OS=`uname`
UOPT=0
GOPT=0
POPT=0
PNAME=
USER=

#determine os
if [ $OS != "Linux" ];	
then
	#hp find
	FIND=/usr/belcan/bin/gfind	
else
	#linux find
	FIND=/usr/bin/find
fi

#Valid Options: u <user>, g(calculate in gb), p <project>
while [ $# -gt 0 ];
do
	case "$1" in
		-g) GOPT=1 ;;
		
		-p) POPT=1
			PNAME="$PROJDIR"
			shift ;;
		
		-u) UOPT=1
			USER="$3"
			shift ;;
	esac

	shift
done

#Check if -p(specify proj directories) option was used, if not, sets to run on all project directories.
if [ $POPT -eq 0 ];
then
	echo "No project directories specified. Script is running for all project areas. This will take a while or you can press ctrl+c to cancel."
	PROJDIR=`/sbin/mount | grep ^/proj | grep direct | cut -d\  -f1 | tr -d \/`
fi
	

#For each file system entered as argument or mounted(if none entered as args)
#find will run for user entered as option. If no user entered as option,
#assumes script was run for current user and uses variable LOGNAME
n=0
for FS in $PROJDIR
do
	(( n+=1 ))
	if [[ $UOPT -eq 1 ]]
	then
		for USE in $3
		do
			rm -f ~/$USE.$n
			#runs as specified user
			$FIND /$FS -user $USE >> ~/$USE.$n
			cat ~/$USE.$n >> ~/$USE.lis
			rm -f ~/$USE.$n
		done
else  
	#runs as current user	
	rm -f ~/$LOGNAME.$n
	$FIND /$FS -user $LOGNAME >> ~/$LOGNAME.$n
	cat $LOGNAME.$n >> ~/$LOGNAME.lis
	rm -f ~/$LOGNAME.$n
	fi
done

#check if *username*.lis is a file, checks that entries aren't directories
#and does a du on files outputting to *username*.du. where *username* 
#is from the arg that should have been entered earlier. If it doesn't exist
#assumes that the script was run for user currently logged in(LOGNAME)  
if [ -f $3.lis ];
	then
		for FILES in `cat ~/$3.lis`
		do
			if [ !-d $FILES ];
			then
				echo "`du $FILES`" >> ~/$3.du
			else
				echo $FILES >> ~/$3.du
			fi
			awk '{ printf "%-15s | %s\n" , $1 , $2 ;}' ~/$3.du
		done
		rm -f ~/$3.lis
	else
		for FILES in `cat ~/$LOGNAME.lis`
		do
			if [ !-d $FILES ];
			then
				echo "`du $FILES`" >> ~/$LOGNAME.du
			else
				echo $FILES >> ~/$LOGNAME.du
			fi
			awk '{ printf "%-15s | %s\n" , $1 , $2 ;}' ~/$LOGNAME.du
		done
		rm -f ~/$LOGNAME.lis
fi

#Totaling disk space and formatting for final presentation
if [ -f ~/$3.du ];
then
	rm -f $3.list
	TOTAL=`awk '{printf $1 "\n"}' ~/$3.du | grep '\([0-9]\)' | paste -s -d + | sed -e 's/+//' | bc`
	echo File-size Path/Name >> ~/$3.txt
	echo "------------------------------------------------------------------------------------" >> ~/$3.list
	cat -n ~/$3.du >> ~/$3.list
	awk '{printf "%-1s %-15s | %s\n" , $1 , $2 , $3;}' ~/$3.list
	rm -f $3.du
else
	rm -f $LOGNAME.list
	TOTAL=`awk '{printf $1 "\n"}' ~/$LOGNAME.du | grep '\([0-9]\)' | paste -s -d + | sed -e 's/+//' | bc`
	echo File-size Path/Name >> ~/$LOGNAME.list
	echo "------------------------------------------------------------------------------------" >> ~/$LOGNAME.list
	cat -n ~/$LOGNAME.du >> ~/$LOGNAME.list
	awk '{printf "%-1s %-15s | %s\n" , $1 , $2 , $3 ;}' ~/$LOGNAME.list
	rm -f ~/$LOGNAME.du
fi

#check for -g option
if [ -f ~/$3.list ];
then
	echo >> ~/$3.list
	if [ $GOPT -eq 1 ];
	then
		echo "Total:"$TOTAL\1024\1024 | bc >> ~/$3.list
	else
		echo "Total:"$TOTAL >> ~/$3.list
	fi
else
	echo >> ~/$LOGNAME.list
	if [ $GOPT -eq 1 ];
	then
		echo "Total:"$TOTAL\1024\1024 | bc >> ~/$LOGNAME.list
	else
		echo "Total:"$TOTAL >> ~/$LOGNAME.list
	fi
fi

#finish
if [ -f ~/$3.list ];
then
	cat ~/$3.list
	echo
	echo "Done. Listing of files is also available in ~/$3.list"
else
	cat ~/$LOGNAME.list
	echo
	echo "Done. Listing of files is also available in ~/$LOGNAME.list"
fi
 
Old 11-29-2004, 05:17 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Use getopts

Code:
while getopts 2>/dev/null r c
do
  case $c in

    r) find . -type f | xargs ls -la |cut -c35-45,59-120 | sort -r -n  ; 
       exit 0 ;;

   \?) echo "\nUsage: $0 [-r]
       list the byte count and name of files in current dir sorted by size
       -r recursively lists all of the subdirectories as well" >&2
        exit 2;;

  esac
done
 
Old 11-29-2004, 06:54 PM   #3
SeT
Member
 
Registered: Dec 2002
Location: cincinnati
Posts: 87

Original Poster
Rep: Reputation: 15
Hmm... I don't know getopts or what most of that is doing. Any help/explanation how the options I need should fit in here?
 
  


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
Command Line Options AUSanders79 Linux - Software 8 03-10-2005 10:24 AM
gcj command line options m3rc_2003 Programming 0 12-03-2003 05:40 AM
kmail command line options randyasu Linux - Software 10 09-30-2003 03:02 AM
WINE and command line options Power User Linux - Software 2 09-14-2003 06:16 PM
Command Line Options in C++ crichards Programming 5 03-08-2003 03:42 PM


All times are GMT -5. The time now is 01:11 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration