LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   BASH question about getting specific formatted display to the screen (https://www.linuxquestions.org/questions/linux-newbie-8/bash-question-about-getting-specific-formatted-display-to-the-screen-4175417664/)

lleb 07-20-2012 03:35 PM

got ya David, that is a bit more clear.

New question for you. I want the script to terminate after running with a -h flag and you hit 'q' to exit the HEREDOC.

Code:

### Options
###################################

while getopts ":nhx" opt
do
        case $opt in
                n )    NO_KILL=true
                        ;;
                x)        set -xvv
                        ;;
                h)        USAGE | less
                        ;;
        esac
done

shift $(( $OPTIND -1 ))

here is the full USAGE() if you are interested.

Code:

USAGE()
{
        cat <<HEREDOC
###########################################################

### Created by Ray Brunkow
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3 of the
# license, at your option.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##########################################################

        This is a RSYNC script designed to keep live data on a backup server.

######### FLAGS TO CALL WITH THIS SCRIPT ###############

        In order to run the rsync.sh script you must first complete the Directions for
creating the rsa key section.  Once that is complete you can then run this script
with three flags as options. 

The three flags are -n, -x, and -h.
        rsync.sh -n                Run this flag if you with to test the script without
                                terminating any RX30 screens and interfaces.  Safe
                                to use while the customer is working.

        rsync.sh -x                Run this flag for debugging the code and or connection.
                                This is no different then calling the script with:
                                bash -xvv rsync.sh.

        rsync.sh -h                Will give you this display.

In addition to running with the above flags you can combine the flags:

        rsync.sh -n -x                This will combine both the -n and -x allowing you to
                                test the script and monitor everything while the customer
                                is still processing scripts.

########## BASIC FUNCTION OF SCRIPT ########################

        The rsync.sh script is designed to create seven sub-folders in the backup servers $HOME
directory.  For a default install of a Rx30 server that path is /usr/rx30.  Each of the seven
sub-folders will be the day of the week to make it easy to locate the most current data
when you have to make the backup server the live server.

ex:        On the backup server you should find the following types of folders:

        /usr/rx30/Monday
        /usr/rx30/Tuesday
        /usr/rx30/Wednesday
        /usr/rx30/Thursday
        /usr/rx30/Friday
        /usr/rx30/Saturday
        /usr/rx30/Sunday

                These will be in the /usr/rx30 directory along with all other
        data that was there when the server had its operating system installed.

###### TO RESTORE DATA #################################

        Depending on the day of the week, you will want to first make a copy
of that days sub-folder into the /tmp directory.  A good idea is to create a directory
in /tmp/rx30_oldserver and just rsync the data over.

***NOTE*** The reason for using rsync instead of cp is that rsync is much lighter
on the server and should be faster.  In addition to that you should be able to continue
working on the server while the rsync is running if you are using screens or have
multiple connections via OpenVPN.

        Below is an example of what that will look like.  Note this example only
contained the rx.dat file to transfer.

[rx30@rx30 ~]$ ls -l /usr/rx30/Friday/
total 864
-rwxrwxr-x  1 rx30 group 879900 Jul 20 16:05 rx.dat
[rx30@rx30 ~]$ ls -l /tmp/
total 48
-rwxrwxrwx 1 rx30  group    10 Jul 13 16:31 HOMEDIR
drwxr-xr-x 3 daemon daemon 4096 Jul 16 08:17 Jetty_0_0_0_0_9090_webapp____-dnguxu
-rwxrwxrwx 1 rx30  group    5 Jul 13 16:31 WHOAMI
drwx------ 2 rx30  group  4096 Jul 12 09:56 acroread_502_505
drwxrwxrwx 2 rx30  group  4096 Jul 13 17:26 gconfd-rx30
drwxr-xr-x 2 daemon daemon 4096 Jul 16 08:17 hsperfdata_daemon
drwx------ 2 rx30  group  4096 Jul 19 13:41 kde-rx30
drwx------ 3 rx30  group  4096 Jul 19 13:42 ksocket-rx30
-rw-r--r-- 1 rx30  group  4133 Jul 10 13:38 netback.txt
drwx------ 2 rx30  group  4096 Jul 19 13:41 orbit-rx30
-rwxrwxrwx 1 rx30  group    0 Jul 13 16:31 proclist
drwx------ 2 rx30  group  4096 Jul 16 08:17 ssh-LTeREr2583
[rx30@rx30 ~]$ mkdir -p /tmp/rx30_oldserver
[rx30@rx30 ~]$ rsync -aviS /usr/rx30/Friday/* /tmp/rx30_oldserver/
building file list ... done
>f+++++++ rx.dat

sent 880126 bytes  received 42 bytes  1760336.00 bytes/sec
total size is 879900  speedup is 1.00
[rx30@rx30 ~]$ ls -l /tmp/rx30_oldserver/
total 864
-rwxrwxr-x 1 rx30 group 879900 Jul 20 16:05 rx.dat

        Once you have the data moved into the /tmp/rx30_oldserver directory.
Download from the Lifeline server txfiles.sh.  This script will copy only the
specific files that need to be moved into the /usr/rx30 directory on the backup server
without issues.  At this point you should be able to fire up Rx30 and verify the data.
        In the backup data either in the sub-folder or the new /tmp/rx30_oldserver
directory you should also find the system configuration files for network, static
routes, printers, etc...  You will need to copy those as root were needed.

[rx30@rx30 ~]$ txfiles.sh

###### DIRECTIONS FOR CREATING RSA KEY################

        Directions for creating the rsa key and making the two
 servers talk to each other without password.

        1st change directory into .ssh and check what files are there.

                [rx30@rx30 ~]$ cd .ssh
                [rx30@rx30 .ssh]$ ls -l
                total 4
                -rw-r--r-- 1 rx30 group 2980 Jun 13 12:02 known_hosts

        2nd create the rsa key.

                [rx30@rx30 .ssh]$ ssh-keygen -t rsa -b 4096
                Generating public/private rsa key pair.
                Enter file in which to save the key (/usr/rx30/.ssh/id_rsa):
                Enter passphrase (empty for no passphrase):
                Enter same passphrase again:
                Your identification has been saved in /usr/rx30/.ssh/id_rsa.
                Your public key has been saved in /usr/rx30/.ssh/id_rsa.pub.
                The key fingerprint is:
                cb:b0:40:c6:e9:f4:9e:f5:71:fc:c3:00:c0:f7:c6:75 rx30@rx30.localdomain

        3rd check that there are two new files with the following permissions

                [rx30@rx30 .ssh]$ ls -l
                total 12
                -rw------- 1 rx30 group 3243 Jun 22 15:50 id_rsa
                -rw-r--r-- 1 rx30 group  743 Jun 22 15:50 id_rsa.pub
                -rw-r--r-- 1 rx30 group 2980 Jun 13 12:02 known_hosts
       
        4th change directory back to the users $HOME

                [rx30@rx30 .ssh]$ cd

        5th copy the key to the remote server

                [rx30@rx30 ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub rx30@XXX.XXX.XXX.XXX
                25
                rx30@XXX.XXX.XXX.XXX's password:
                Now try logging into the machine, with "ssh 'rx30@XXX.XXX.XXX.XXX'", and check in:

                .ssh/authorized_keys

                to make sure we haven't added extra keys that you weren't expecting.

        6th, follow directions on the screen.

                [rx30@rx30 ~]$ ssh rx30@XXX.XXX.XXX.XXX
                Last login: Fri Jun 22 14:12:08 2012 from 10.10.4.77
                [rx30@rx30 ~]$ exit
                logout
                Connection to XXX.XXX.XXX.XXX closed.

Now the rsync.sh script will work without interaction.  THIS IS A MUST DO.
HEREDOC
}

What I want to happen is when you hit 'q' after reading this for the script to exit and NOT continue to run. I attempted to put an exit after HEREDOC at the end of the function. that was bad. I know I can not put an exit after the } close of the function, but can I put an if statement of some type there?

I also tried a 2nd pipe after the less in the getops

Code:

h)    USAGE | less | exit;;
this terminated the script asap without putting the less to the screen. not quite what i want.

lleb 07-23-2012 08:57 AM

fixed. just replaced the exit 2;; with exit 0 at the end of the USAGE() after the bottom HEREDOC before }.

Thanks again all for the help.


All times are GMT -5. The time now is 08:35 PM.