LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-24-2018, 12:19 PM   #1
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Rep: Reputation: 16
Question BASH script with goto or if statements?


Hello all!

I am writing a script that will umount/mount certain batches of mounts within fstab. I am trying to have it all within one script with a little sub menu but I am having some questions about that.

The general syntax should be something like:

What would you like to remount?

1. Windows Shares
2. Linux NFS Shares
3. Printer folders

Based upon what they select, will run different commands. I was thinking there was a goto command, but I can't seem to find one. Would I just nest them in if statements? If the number is 1 do this else do nothing. If 2 do this, else do nothing. If 3 do this, else do nothing.

I was also going to use the mailx command to email me whenever someone runs this command.

Any tips?
 
Old 09-24-2018, 01:25 PM   #2
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Quote:
If the number is 1 do this else do nothing. If 2 do this, else do nothing. If 3 do this, else do nothing.
Quick examples:
Code:
read -p "Enter 1 2 or 3 :" num
if [ "$num" -eq "1" ]; then
    echo "You entered 1"
elif [ "$num" -eq 2 ]; then
    echo "You entered 2"
elif [ "$num" -eq 3 ]; then
    echo "You entered 3"
else echo "You entered a number out of range"
fi
Code:
options="Quit WindowsShares LinuxNFSShares Printerfolders"
select opt in $options; do
    case $opt in
        Quit) clear; exit;;
    
        WindowsShares) echo "Windows Shares" ;;
        
        LinuxNFSShares) echo "Linux NFS Shares" ;;
        
        Printerfolders) echo "Printer folders" ;;
    esac
done
 
1 members found this post helpful.
Old 09-24-2018, 02:35 PM   #3
tarken
Member
 
Registered: Jan 2010
Location: Portland
Distribution: Kubuntu
Posts: 82

Original Poster
Rep: Reputation: 16
How do I use that bottom part?

This is what I have right now"

Code:
echo "What would you like to do?"
echo "1. Re mount Windows shares"
echo "2. Re mount NFS shares"
echo "3. Re mount all shares"


read -p "Enter 1, 2, or 3:" num
if [ "$num" -eq "1" ]; then
    echo "You entered 1"
elif [ "$num" -eq 2 ]; then
    echo "You entered 2"
elif [ "$num" -eq 3 ]; then
    echo "You entered 3"
else echo "You entered a number out of range"
fi

##########################################################
# Forcibly unmount all CIFS/Windows shares in /etc/fstab #
##########################################################

echo "Trying to unmount all CIFS/Windows shares"

umount -t cifs


#########################################################
# Remount all CIFS/Windows shares in /etc/fstab #########
#########################################################

echo "Trying to mount all CIFS/Windows shares in /etc/fstab"

mount -a -t cifs


#####################################################
# Forcibly unmount all mount points in /etc/fstab ###
#####################################################

echo "Trying to forcibly unmount all mount points in /etc/fstab, please wait"

umount -a -f

#####################################################
# Remount all shares listed in /etc/fstab ###########
#####################################################

echo "Trying to mount all mount points in /etc/fstab, this should only take a few seconds"

mount -a

echo "If you did not get any errors, you are good to go. If you did get errors, please note them and let the team know"

How do I have it run only the part for which they select?
 
Old 09-24-2018, 05:07 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Those were examples on how to select an option. You'll going need to study bash a little.
http://mywiki.wooledge.org/BashFAQ
http://www.google.com/search?ie=ISO-...eference&gbv=1

Haven't checked any of those mount commands, looking at your script...

Here is a template using case, you'll have to write your script to do what you want done.

Code:
#! /usr/bin/env bash

clear

# PS3 prompt message
PS3="Select an option.:  "

#Make an array with options, quote spaced items.
options=(Quit "Mount Windows Shares" "Unmount Windows Shares" 
            "Mount NFS Shares" "Unmount NFS Shares")
            
#Make a selection menu for array
select opt in "${options[@]}"; do
    #case statements
    case $opt in
        Quit) clear
                exit
        ;;
    
        "Mount Windows Shares") echo "Mounting Windows Shares" 
                            #<1st comand here>
                            #<2nd command here>
        ;;
        
        "Unmount Windows Shares") echo "Unmounting Windows Shares" 
                            #<commands here>
                            #<commands here>
        ;;
                        
        "Mount NFS Shares") echo "Mounting NFS Shares"  
                            #<commands here>
        ;;
        
        "Unmount NFS Shares") echo "Unmounting NFS Shares"
                            #<commands here>
        ;;
    esac
done
You can also use if, then, else, elif in numerous ways.

Code:
echo "What fruit do you like best?"
read -p "apple, pear, or other? " answer

if test "$answer" = "apple"; then
    echo "Apple is a good fruit."
elif test "$answer" = "pear"; then
    echo "Pear is a sweet fruit"
else
    echo "Other fruit is good too."
fi
Code:
echo "What fruit do you like best?"
OPTIONS="apple pear other quit"

select opt in $OPTIONS; do
    if [ "$opt" = "apple" ]; then
        echo "Apple is a good fruit."
    elif [ "$opt" = "pear" ]; then
        echo "Pear is a sweet fruit."
    elif [ "$opt" = "other" ]; then
        echo "Other fruit is good too."
    elif [ "$opt" = "quit" ]; then
        exit
    fi
done
You can also echo your commands first to see what the output would be
Code:
echo "mount -t cifs ......"
See:
man mount
man echo
read -h
 
  


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
[SOLVED] Problem with IF ELSEIF and GOTO statements in Fortran faizlo Programming 1 12-29-2013 03:05 PM
BASH goto dkrysak Programming 35 03-21-2013 10:22 AM
Case statements and vars for bash script geech Programming 1 01-09-2009 04:49 PM
can you use goto in bash script sabliny Programming 3 10-07-2005 05:54 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

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

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