Suggestions/cleanup Bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suggestions/cleanup Bash script
# 1  
Old 04-27-2009
Suggestions/cleanup Bash script

Hello, beginner bash scripter here.. I was able to write a script and it works just fine. I'm just wondering if someone could chime in or any suggestions to make it cleaner or tighter so to speak. I have a disk to disk backup solution which uses 250GB disks. When one gets full I just po in a new one. the backup disk is also shared via NFS automount.
Code:
#!/bin/bash
#Unexport all nfs directories
echo unexporting nfs directories!
exportfs -au
#check to make sure unexport was successful!
exportfs -v
#umount BACKUPS
umount -v /BACKUPS
#replace the old drive with a new one
echo "Do you wish to continue? (y/n)"
read ans

case $ans in
     Y|y) ;;
[Yy][Ee][Ss]) ;;
     N|n) exit ;;
[Nn][Oo]) exit ;;
       *) echo "Invalid command"
esac
#partiton and create new filesystem on the drive
# run fdisk to make sure we are using /dev/sdc
echo "RUNNING FDISK TO SEE IF /DEV/SDC IS THE CORRECT DRIVE!"
fdisk -l

echo "Are you ready to run fdisk? (y/n)"
read ans

case $ans in
     Y|y) ;;
[Yy][Ee][Ss]) ;;
     N|n) exit ;;
[Nn][Oo]) exit ;;
       *) echo "Invalid command"
esac
echo "ABOUT TO CREATE THE NEW PARTITION!!!!!!!!!!!!!"
fdisk /dev/sdc < ~/250GB_format.txt

#check to see if the OS automounted the disk
echo #CHECKING TO SEE IF FEDORA AUTOMOUNTED THE DRIVE!!!!!!!!!!!!!!!!"
umount -v /dev/sdc1

#create the new filesystem
echo "ABOUT TO CREATE THE FILESYSTEM!!!!!!!!!!!!!!!!!!!"
mkfs.ext3 /dev/sdc1

#mount the new file system
echo "MOUNT THE FILESYSTEM!!!!!!!!"
mount -v /dev/sdc1 /BACKUPS


#run df to see that the filesystem is mounted an reporting the correct size
df -h

echo "Is the disk mounted and showing the correct partition size? (y/n)"
read ans

case $ans in
     Y|y) ;;
[Yy][Ee][Ss]) ;;
     N|n) exit ;;
[Nn][Oo]) exit ;;
       *) echo "Invalid command"
esac
#get the UUID to add to fstab
echo "GRAB THE BLOCKID"
blkid /dev/sdc1

#export the filesystems
echo "EXPORTING /BACKUPS"
exportfs -av


Last edited by otheus; 04-28-2009 at 11:09 AM.. Reason: added code tags
# 2  
Old 04-28-2009
Yeah... when you are prompted for "continue", and you accidentally hit "m" instead of "n", there goes your hard drive!
# 3  
Old 04-28-2009
Quote:
Originally Posted by otheus
Yeah... when you are prompted for "continue", and you accidentally hit "m" instead of "n", there goes your hard drive!
I don't see how that would be the case.....Please be more specific..
# 4  
Old 04-28-2009
Okay, when you are prompted for "ready to run fdisk?" and you hit "m" or "b" instead of "n", the code simply continues to the next line.
# 5  
Old 04-28-2009
Quote:
Originally Posted by otheus
Okay, when you are prompted for "ready to run fdisk?" and you hit "m" or "b" instead of "n", the code simply continues to the next line.
Ahh..OK...but doesn't this line prevent that?

*) echo "Invalid command"
# 6  
Old 04-28-2009
What about putting your yes no question in a function and calling it with a question string and checking the result before moving on?

e.g

Code:
yes_no()
{
        message="$1"
        while :
        do
              echo "$message [y,n] (default n)"
              read ans
              case ${ans:=n} in
                    N*|n*) return 1 ;;
                    Y*|y*) return 0 ;;
                    *) echo "You have entered an invalid option. Please pick correctly"
              esac
        done
}

Then call your function like this :-

Code:
if yes_no "Do you want to continue?" ;then
        # Got yes so run fdisk
         fdisk blah blah....
else
        # Exit
        echo "You chose no, or a problem has occurred"
        exit 2
fi

.....


Last edited by lavascript; 04-28-2009 at 12:24 PM.. Reason: typo in function
# 7  
Old 04-28-2009
Quote:
Originally Posted by woodson2
Ahh..OK...but doesn't this line prevent that?

*) echo "Invalid command"
Unfortunately not as you are not in a loop or checking a valid status anywhere.

Basically anything other than the options you define will exit the case statement and echo your message and continue with the script.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX script for cleanup

Hello, I need some help from unix guru's here..I am looking for some advanced level script to cleanup the directories and files from specific directories under a file system.. The folders are created under /opt/modules And under modules, there are multiple subfolders with the application... (6 Replies)
Discussion started by: mb525
6 Replies

2. Shell Programming and Scripting

Suggestions on this script please

i=1 out="" j=`expr 2 * $1` while do out="$out"#"" echo $out ((i=i+1)) done while do print ${out%?} ((i=i+1)) done This script is throwing an error: gurnish:/home/fnb/gurnish/saurabh/scripts> while1 3 expr: 0402-050 Syntax error. # (6 Replies)
Discussion started by: targetshell
6 Replies

3. Shell Programming and Scripting

Suggestion with script to cleanup

I need help with sed and awk scripts to search for Symmetrix ID=000090009902 and then grep its child disk devices associated to the dead paths and display them only, so that those dead devices can be removed. test01:/#powermt display dev=all Pseudo name=hdiskpower0 Symmetrix ID=000090009902... (0 Replies)
Discussion started by: aix_admin_007
0 Replies

4. Shell Programming and Scripting

pid.cleanup script.

Hi guys! I have a directory in the production environment from which i have to delete files older then 40 minutes with .pid extention. I wrote a script below for the purpose. #!/bin/bash # # Script to delete specific file older than N minutes. # OLDERTHAN="40" #40 minutes ... (6 Replies)
Discussion started by: sajid.shah
6 Replies

5. UNIX for Advanced & Expert Users

Table Cleanup Script

I needed some help with a script to fetch and delete all records prior to 3 days from now connecting to sybase from sunos. I wrote the following script but not working..can someone please guide me with my code. Thanks #!/bin/ksh ##GET PREVIOUS DAY DATE dt=`date | awk... (3 Replies)
Discussion started by: moe458
3 Replies

6. Shell Programming and Scripting

Mail cleanup from ksh script, keeping 50 most recent msgs

I found some posts describing how to completely clean out a mailbox in Unix/Linux. But I want to keep the 50 most recent messages. Any ideas out there? Thanks! (3 Replies)
Discussion started by: OPTIMUS_prime
3 Replies

7. Shell Programming and Scripting

Cleanup script

Hi! I would like to write a script which remove some files, all beginning with the same prefix : prefix.1 doc/prefix.2 ../prefix.3 etc. So, I would create a file and chmod it executable. But I dont know how to pass a variable to a script. I would like to write something like ... (2 Replies)
Discussion started by: tipi
2 Replies

8. Shell Programming and Scripting

User Cleanup Script

Hi Guys, I've got an system setup to act as an sftp server. I have a script that allows me to create chroot users running a custom shell within their home directory, it also creates a subdirectory that they can write into. I'm trying to write a script (that I can cron at a later date) that checks... (3 Replies)
Discussion started by: King_Brucie
3 Replies

9. Shell Programming and Scripting

awk/sed/ksh script to cleanup /etc/group file

Many of my servers' /etc/group file have many userid's that does not exist in /etc/passwd file and they need to be deleted. This happened due to manual manipulation of /etc/passwd files. I need to do this for 40 servers. Can anyone help me in achieving this? Even reducing a step or two will be... (6 Replies)
Discussion started by: pdtak
6 Replies

10. Shell Programming and Scripting

Hangman written in Bash. Suggestions please...

qwertyuiop (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question