Register
It is currently Fri Nov 28, 2014 9:45 am

Need Help with Script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sat Mar 26, 2011 10:56 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Hello all-

Having a long day and need help with the following basic function.

I would like to achieve is to run this in a directory and unzip all gzip files and exit.

What I don't want is for you to write the script for me, just a hint.

What happens when I run the script in my home directory it works and I get good results.

However, when I run this in a directory where the files actually live I get the following error:

Code:
find: paths must precede expression: antimalware.errors1103190003.log.gz
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]


Code:
#!/bin/bash

# Will unzip all gzip files in current working directory with any of the following options.

AM="*antimalware*.gz"
PWD=`pwd`

echo ""
echo "Please select one of the followin options to unzip the following log files in your current working directory"
echo ""
echo "1: Antimalware"
echo "2: Coordinator"
echo "3: Core"
echo "4: Log manager"
echo "5: Sysconfig"
echo "6: User Interface"
echo ""

antimal () {
    for p in $PWD; do
      find $p -type f -name $AM
done
}

read number

#for i in `ls -X`; do
#    gzip -d $i
#done 

if [[ $number == 1 ]]; then
  antimal;

elif [[ $number == 2 ]]; then
$PWD

elif [[ $number == 3 ]]; then
$PWD

elif [[ $number == 4 ]]; then
$PWD

elif [[ $number == 5 ]]; then
$PWD

elif [[ $number == 6 ]]; then
$PWD
fi


Update:
Ok, I don't know what I was thinking, but I think I fixed this with the following modification:

Code:
antimal () {
    for i in $AM;do
    gzip -d $i
done
}


Update:
So I have this working script now. I just feel like its too bloated and its ofcourse only a beginer script.

Can someone please help me streamline my script. I want to be able to have only one fuction that can take my selected option and give me results based on my selection; again, without all those cluttered functions.

Code:
#!/bin/bash

# Will unzip all gzip files in current working directory with any of the following options.

AM="*antimalware*.gz"
COO="*coordinator*.gz"
COR="*core*.gz"
LOG="*log*.gz"
SYS="*sysconfig*.gz"
UI="*ui*.gz"

echo ""
echo "Please select one of the followin options to unzip the following log files in your current working directory:"
echo ""
echo "1: Antimalware"
echo "2: Coordinator"
echo "3: Core"
echo "4: Log manager"
echo "5: Sysconfig"
echo "6: User Interface"
echo ""

# Functions
antimal () {
    for a in $AM;do 
      gzip -d $a
done
}

coord () {
    for c in $COO;do
      gzip -d $c
done
}

cor () {
    for co in $COR;do
      gzip -d $co
done
}

log () {
    for l in $LOG;do
      gzip -d $l
done
}

sys () {
    for s in $SYS;do
      gzip -d $s
done
}

useri () {
    for u in $UI;do
      gzip -d $u
done
}

# Script
read number

if [[ $number == 1 ]]; then
  antimal;

elif [[ $number == 2 ]]; then
  coord;

elif [[ $number == 3 ]]; then
  cor;

elif [[ $number == 4 ]]; then
  log;

elif [[ $number == 5 ]]; then
  sys;

elif [[ $number == 6 ]]; then
useri;
fi


Top
 Profile  
 PostPosted: Sun Mar 27, 2011 1:03 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
How about creating one function called 'unzip()', which takes parameters, then your if-then-else (you could also use a case-switch, which in my opinion would be preferable) can call the unzip function with with either $AM, $COO, $COR, $whatever as a parameter and unzip() will unzip anything it got from the argument list ($*)


Top
 Profile  
 PostPosted: Sun Mar 27, 2011 2:19 am   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Patsie wrote:
How about creating one function called 'unzip()', which takes parameters, then your if-then-else (you could also use a case-switch, which in my opinion would be preferable) can call the unzip function with with either $AM, $COO, $COR, $whatever as a parameter and unzip() will unzip anything it got from the argument list ($*)



Thanks Patsie-

My script definately needs works, but I know have relearned the case command.

Code:
#!/bin/bash

# Will unzip all gzip files in current working directory with any of the following options.

AM="*antimalware*.gz"
COO="*coordinator*.gz"
COR="*core*.gz"
LOG="*log*.gz"
SYS="*sysconfig*.gz"
UI="*ui*.gz"

echo ""
echo "Please select one of the followin options to unzip the following log files in your current working directory:"
echo ""
echo "1: Antimalware"
echo "2: Coordinator"
echo "3: Core"
echo "4: Log manager"
echo "5: Sysconfig"
echo "6: User Interface"
echo ""

# Function
unzip () {
      gzip -d $*
}

read option

case "$option" in
'1')
unzip $AM;
echo "done"
;;
'2')
unzip $COO;
echo "done"
;;
'3')
unzip $COR;
echo "done"
;;
'4')
unzip $LOG;
echo "done"
;;
'5')
unzip $SYS;
echo "done"
;;
'6')
unzip $UI;
echo "done"
;;
'')
echo "Usage: $0 (selecting one of the following options [1-6])"
;;
esac


Top
 Profile  
 PostPosted: Sun Mar 27, 2011 3:06 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Code:
'')
echo "Usage: $0 (selecting one of the following options [1-6])"
;;

I assume with this last case you want to have a default/catch-all clause, so if someone enters anything invalid, it will show this usage message. In that case I would replace the 2 single quotes with an asteriks/star like so:
Code:
*) echo "Usage: $0 (selecting one of the following options [1-6])"
Also, because this is the last switch of the case, the breaking double semi-colon isn't needed anymore.
If you move the 'echo Done' to the unzip() function, you can shorten the switches of your case like so:
Code:
'5') unzip $SYS ;;
'6') unzip $UI ;;


Top
 Profile  
 PostPosted: Sun Mar 27, 2011 6:39 am   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Patsie-

Thanks again!

You've really helped and I feel it looks like a decent script now.

Code:
#!/bin/bash
# Will unzip all gzip files in current working directory with any of the following options.

AM="*antimalware*.gz"
COO="*coordinator*.gz"
COR="*core*.gz"
LOG="*log*.gz"
SYS="*sysconfig*.gz"
UI="*ui*.gz"

echo ""
echo "Please select one of the followin options to unzip the following log files in your current working directory:"
echo ""
echo "1: Antimalware"
echo "2: Coordinator"
echo "3: Core"
echo "4: Log manager"
echo "5: Sysconfig"
echo "6: User Interface"
echo ""

# Function
unzip () {
      gzip -d $*
echo "done"
}

read option

case "$option" in
'1')
unzip $AM ;;
'2')
unzip $COO ;;
'3')
unzip $COR ;;
'4')
unzip $LOG ;;
'5')
unzip $SYS ;;
'6')
unzip $UI ;;
*)
echo "Usage: $0 (selecting one of the following options [1-6])"
esac


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP