LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-25-2018, 04:09 AM   #1
Rosain
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Rep: Reputation: Disabled
Multiple inputs and if statements in shell script


Hey, im very new to shell scripting and Linux in general.
I have a cron job for a backup script that runs once a night and I want to improve that shell script and add inputs and if statements and other stuff.
I did some reading and I couldn't find what I wanted so I'm going to post a mock script with the process I want to run.

Code:
#!/bin/bash
FOLDER1SRC="/home/username/directory/foldertobackup1"
FOLDER1DEST="/home/username/directory/foldersbackuped1"
FOLDER2SRC="/home/username/directory/foldertobackup2"
FOLDER2DEST="/home/username/directory/foldersbackedup2"
FILENAME1=foldername-folder1-bkp-$(date +%-Y%-m%-d)-$(date +%-T).tgz
FILENAME2=foldername-folder2-bkp-$(date +%-Y%-m%-d)-$(date +%-T).tgz
STOP=quit
CONTINUE=yes
NEXT=no
echo "If this is a manual run, type yes"
wait 10s for an input, if no input skip to AUTOBACKUP
while :
do
 read INPUT_STRING
 case $INPUT_STRING in
    yes)
       echo "Hi there! Welcome to my backup script!"
	   sleep 1s
	   echo "Are we backing up folder1?"
	    if $CONTINUE
		 do 
		  echo "This could take over an hour"
		  cd $WORLDDIR
		  echo "Archiving files to $FOLDER1DEST"
		  tar --exclude='./sub_folder1' --exclude='./sub_folder2' --exclude='./sub_folder3' --exclude='./sub_folder3' --exclude='./sub_folder4' -zcvf $FOLDER1SRC$FILENAME1 $FOLDER1DEST
		  echo "Archiving finished and stored in $FOLDER1SRC, would you like to upload this archive to google?"
		   if $CONTINUE 
		   do gsutil rsync -r /home/folder/directory gs://folder-directory/folder1
		   else done
		   echo "upload finished, would you like to remove any old backups?"
		    if $CONTINUE 
			echo "deleting backups from $FOLDER1DEST"
			rm -r /home/username/directory/folder1dest/*
		     else done
			echo "backups deleted, would you like to back up FOLDER2 as well?"
			if $CONTINUE
              continue to no)
			else exit
	no)
	   echo "Are we backing up folder2?"
           if $CONTINUE
            do
		      cd $FOLDER2DIR
		      echo "Archiving files to $FOLDER2DEST"
		      tar -zcvf $FOLDER2SRC$FILENAME2 $FOLDER2DEST
		      echo "Archiving finished and stored in $FOLDER2DEST, would you like to upload this archive to google?"
		       if $CONTINUE 
		       do gsutil rsync -r /home/folder/directory gs://folder-directory/folder2
		       else done
		       echo "upload finished, would you like to remove any old backups?"
		       if $CONTINUE 
			   echo "deleting backups from $FOLDER2DEST"
			   rm -r /home/username/directory/folder2dest/*
			   sleep 1s
			   echo "files deleted, script will now close"
		     exit 
			else exit
	AUTOBACKUP)
	 cd $FOLDER2DIR
     tar -zcvf $FOLDER2SRC$FILENAME2 $FOLDER2DEST
     do gsutil rsync -r /home/folder/directory gs://folder-directory/folder2
	 sleep 10m
	 rm -r /home/username/directory/folder2dest/*
	 exit
Please keep in mind, I have next to no idea what im doing. So I don't expect my mock-up to work at all but its an example of what I'd like to accomplish, could someone guide me?
 
Old 09-25-2018, 04:35 AM   #2
lougavulin
Member
 
Registered: Jul 2018
Distribution: Slackware,x86_64,current
Posts: 279

Rep: Reputation: 100Reputation: 100
A cron script has to be autonomous. It can not ask user inputs.
You can add a config file and modify this config file to make it do something else.
Or, still using cron, launch it several times with different parameters at different times.
 
Old 09-25-2018, 04:42 AM   #3
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
What guidance do you need?

Quote:
echo "If this is a manual run, type yes"
wait 10s for an input, if no input skip to AUTOBACKUP
Let me just mention that you can't use inputs in a cron script.

What strikes me: After the prompts, you don't read any answer. And the read at the beginning of the while loop has no prompt. STOP and NEXT are not used. CONTINUE is always "yes".

I don't quite understand why you need a loop. Does the script continuously backup FOLDER1 and FOLDER2?

Syntax errors: The ifs have neither then nor fi. while and case are not closed. else done won't work.
 
Old 09-25-2018, 04:54 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,698

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
The OP stated they were new to programming and it was just a mock up i.e. pseudo code. I assume the idea was to use the same script if running from cron or manually via the command line. bash read does have a timeout option. I agree the infinite while loop serves no purpose.

Instead of asking a bunch of questions you could use a few command line arguments. Using getopts would make it easier to parse the arguments.

http://wiki.bash-hackers.org/howto/getopts_tutorial

Last edited by michaelk; 09-26-2018 at 07:34 AM.
 
Old 09-26-2018, 04:23 AM   #5
Rosain
LQ Newbie
 
Registered: Sep 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
The OP stated they were new to programming and it was just a mock up i.e. pseudo code. I assume the idea was to use the same script if running from cron or manually via the command line. bash read does have a timeout option. I agree the infinite while loop serves no purpose.

Instead of asking a bunch of questions you could use a few command line arguments. Using getopts would not make it easier to parse the arguments.

http://wiki.bash-hackers.org/howto/getopts_tutorial
Yes, this is a poster who understands my OP, thank you for the link. I'll read up a bit more.
 
Old 09-26-2018, 07:40 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,698

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Quote:
Using getopts would not make it easier to parse the arguments.
Hmm. I think you understood what I was trying to write and that getopts would make it easier to parse the command line arguments. Using none or a specific option could be used to setup an auto backup when running via cron.
 
Old 09-27-2018, 12:54 AM   #7
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
wait 10s for an input, if no input skip
under bash, the builtin 'read' command has a timeout option:
Code:
help read
that's just one bit of advice, i'm sure you need more.
 
  


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] passing multiple inputs to script that calls other script(s) glamiss Linux - General 3 05-02-2012 01:32 AM
[SOLVED] Run script from another with multiple user inputs and expect ! zoonose Linux - Newbie 2 06-05-2011 08:57 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM
shell script inputs aloishis89 Programming 3 03-10-2008 02:23 AM
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 - Newbie

All times are GMT -5. The time now is 06:14 PM.

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