LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-14-2011, 09:49 PM   #1
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Rep: Reputation: Disabled
Smile Shell script to prompt for input


Hi,

I would like to run some existing scripts and send it to a text file:

Note: 'script' is an exist shell script
Note: '/opt/2011jun15/my.db' auto generates everyday with only changes to the directory 2011jun15 base on the current date

currently i will need to run the script manually and make changes to the path below, change directory name 2011jun15 daily and text file new2011jun15.txt

./script -f /opt/2011jun15/my.db > new2011jun15.txt

Am i able to write a shell script to prompt me for a change of path for only '2011jun15' and 'new2011jun15.txt'?

It will be great if i can automate the whole process?

Thanks in advanced.
 
Old 06-14-2011, 10:37 PM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Yes ... have a look at your favourite documentation and look for command line parameters (they look like - $1, $2 ...)
 
Old 06-16-2011, 04:12 AM   #3
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Yes ... have a look at your favourite documentation and look for command line parameters (they look like - $1, $2 ...)
But how do I change the path of the directory so that it gives me the outout I want base on the date? No idea how to change 2011jun15 to another date.

./script -f /opt/2011jun15/my.db > new2011jun15.txt

Last edited by kent2612; 06-16-2011 at 04:15 AM.
 
Old 06-16-2011, 04:32 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
You can retrieve the current date using the date command and put it in the format you want (except you have to translate the uppercase month to lowercase), e.g.
Code:
./script -f /opt/$(date +%Y%b%d | tr [A-Z] [a-z])/my.db > new$(date +%Y%b%d | tr [A-Z] [a-z]).txt
Hope this helps.
 
1 members found this post helpful.
Old 06-16-2011, 07:17 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
So I am not sure if colucix's answer is what you are after or are you just asking how to change entry A with entry B?

So to use your example, do you no longer wish to call the script with -f and now just enter the new portion of the path so it is read that
way in the script?

For example:
Code:
./script 2011jun16
So here you would like the script to take the parameter and replace it internally as appropriate?

If so, inside 'script' you can have:
Code:
if (( $# != 1 ))
then
    echo "Add your own appropriate message to say must have only one parameter"
else
    IN_FILE=/opt/$1/my.db
    OUT_FILE=new${1}.txt
fi
Maybe this will help?
 
1 members found this post helpful.
Old 06-20-2011, 01:36 AM   #6
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
You can retrieve the current date using the date command and put it in the format you want (except you have to translate the uppercase month to lowercase), e.g.
Code:
./script -f /opt/$(date +%Y%b%d | tr [A-Z] [a-z])/my.db > new$(date +%Y%b%d | tr [A-Z] [a-z]).txt
Hope this helps.
Great thanks!
 
Old 06-20-2011, 01:39 AM   #7
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So I am not sure if colucix's answer is what you are after or are you just asking how to change entry A with entry B?

So to use your example, do you no longer wish to call the script with -f and now just enter the new portion of the path so it is read that
way in the script?

For example:
Code:
./script 2011jun16
So here you would like the script to take the parameter and replace it internally as appropriate?

If so, inside 'script' you can have:
Code:
if (( $# != 1 ))
then
    echo "Add your own appropriate message to say must have only one parameter"
else
    IN_FILE=/opt/$1/my.db
    OUT_FILE=new${1}.txt
fi
Maybe this will help?
Thanks, I'll see if it works, will let you know again.
 
Old 06-22-2011, 10:33 PM   #8
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
I am trying another option to input the date manually.

I need the input to be in title case, for example if i enter 2011jun23 it will read it as 2011Jun23. This is to ensure that the path is /opt/2011Jun23/my.db

#!/bin/bash
echo "Enter the date"
read i
./script -f /opt/$1/my.db > new$1.txt
 
Old 06-23-2011, 12:20 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
So is there a question or you are just informing us of your choice?
 
Old 06-23-2011, 12:32 AM   #10
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
So is there a question or you are just informing us of your choice?
Oops sorry it is suppose to be a question to ensure I get the correct date format I want. Thanks
 
Old 06-23-2011, 02:28 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Well with a little playing the not so intuitive answer is, you have to alter (if I am wrong hopefully someone will say)
the input to get the desired output:
Code:
#!/bin/bash

my_date=$(date -d "$1" +%Y%b%d)

./script -f /opt/$my_date/my.db > new${my_date}.txt
And then you need to call it like so:
Code:
./new_script.sh 06jun2011
I would also point out that your example is flawed as you store the information in 'i' but the never use it and rather try to call $1.

I think it would still be easier to include this testing / input in your original script.
 
Old 06-23-2011, 02:38 AM   #12
kent2612
LQ Newbie
 
Registered: Jun 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Well with a little playing the not so intuitive answer is, you have to alter (if I am wrong hopefully someone will say)
the input to get the desired output:
Code:
#!/bin/bash

my_date=$(date -d "$1" +%Y%b%d)

./script -f /opt/$my_date/my.db > new${my_date}.txt
And then you need to call it like so:
Code:
./new_script.sh 06jun2011
I would also point out that your example is flawed as you store the information in 'i' but the never use it and rather try to call $1.

I think it would still be easier to include this testing / input in your original script.

Some mistakes in my previous script, it is suppose to be $i instead of $1, btw I would like to have the option to enter the date I want and would want to ensure that it turns out to be the correct format I want eg.. 2011Jun23 (Title case for Jun), Thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Shell Script to prompt customer LinuxScript Linux - Newbie 7 02-03-2011 12:34 AM
Using shell command output as input in shell script - how to do? EnderX Linux - Newbie 2 06-30-2010 12:46 PM
Shell script sudo without password prompt under Darwin? lhouk Other *NIX 3 02-08-2008 11:16 AM
how can I take input with a shell script? GUIPenguin Linux - General 4 04-11-2005 03:46 PM
Shell script user input tuckermaddox Linux - Newbie 5 08-12-2004 03:14 AM


All times are GMT -5. The time now is 12:57 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration