Thursday, July 30, 2009

Unix prompts?

Hi,


Is there a way to automate unix user prompts?


My code is set to "read" input and store it as a variable.


is there a way to set it that it is able to be entered when telling the program to run?


I.E. :


./myprog.sh variable1

Unix prompts?
You can pass parameters to a shell script by supplying them on the command line, and then use them in the script as $1 through $9.





For example:





sample.sh abc 123 xyz 456





The sample.sh is the script name and it has four passed parameters:





$1 = abc


$2 = 123


$3 = xyz


$4 = 456





These can be used as follows:





DATA_FILE=$1


echo "Processing data file called $DATA_FILE"





You can also figure out how many parameters were passed by using the $# as a value, or get a list of all of them using $*





And if you need to pass more than nine (9) parameters, you can look into the "shift" command. For example, if I have these:





sample.sh 1 2 3 4 5 6 7 8 9 10 11 12





I can read the first $1 (a value of 1), then type in "shift" and all of the values will be shifted over by one, so the next time I look at $1 it will contain a value of 2. It is important to read the values BEFORE you issue the shift because the first value will be dropped and you won't have any further access to it unless it was saved in a variable within the script.





Hope this helps.
Reply:Yes. Put the line:





variable1 = $1;





into the shell script.
Reply:Your question is not very clear.


I am assuming you want to change the user prompt.


From the program name, it looks like you are using bourne shell.


If you want to change the prompt in bourne shell, use


PS1="mynewprompt"


If that is not what you want, on unix do


man sh


It should tell you everything you need to know about sh.


No comments:

Post a Comment