LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell script to prompt for input (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-prompt-for-input-886392/)

kent2612 06-14-2011 09:49 PM

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.

grail 06-14-2011 10:37 PM

Yes ... have a look at your favourite documentation and look for command line parameters (they look like - $1, $2 ...)

kent2612 06-16-2011 04:12 AM

Quote:

Originally Posted by grail (Post 4385961)
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

colucix 06-16-2011 04:32 AM

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.

grail 06-16-2011 07:17 AM

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?

kent2612 06-20-2011 01:36 AM

Quote:

Originally Posted by colucix (Post 4387332)
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!

kent2612 06-20-2011 01:39 AM

Quote:

Originally Posted by grail (Post 4387441)
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.

kent2612 06-22-2011 10:33 PM

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

grail 06-23-2011 12:20 AM

So is there a question or you are just informing us of your choice?

kent2612 06-23-2011 12:32 AM

Quote:

Originally Posted by grail (Post 4393414)
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

grail 06-23-2011 02:28 AM

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.

kent2612 06-23-2011 02:38 AM

Quote:

Originally Posted by grail (Post 4393471)
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


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