Click to See Complete Forum and Search --> : script for prompt selection
supercoffee
11-18-2003, 10:26 AM
I write a script for prompt selection as follow
select test in prompt1 prompt2 prompt3
do
printf='please select a prompt here ==> '
case $test in
prompt1) PS1=$test;break ;;
prompt2) PS1=$test;break ;;
prompt3) PS1=$test;break;;
*) echo "error invalid $REPLY" ;;
esac
done
I am using redhat8 and bash!
after I have made the selection, nothing change with the prompt!
What the problem is? Thanks!
the script doesn't do anythign at the moment - im assuming it is supposed to be like that. i don't get any errors when running it.
you need to add the following as the first line though:
#!/bin/bash
supercoffee
11-18-2003, 10:50 AM
nothing change after adding #!/bin/bash
after running ths script, and make the selection, no change in prompt, no error.
What is the problem? Thanks!
if you want to actually change the value of the PS1 variable you have to export it.
i.e.
export PS1="insert whatever you want the prompt to be here"
select test in prompt1 prompt2 prompt3
do
printf='please select a prompt here ==> '
case $test in
prompt1) PS1=$test;break ;;
prompt2) PS1=$test;break ;;
prompt3) PS1=$test;break;;
*) echo "error invalid $REPLY" ;;
esac
done
the more i look at this - it is just weird.
what exactly are you trying to do? is the script you posted just a part of a larger script?
are you going to have a real valid prompt string in place of the prompt1, prompt2, and prompt3? that would make more sense to me. if so then you just need to exoprt the PS1 variable as i stated in my post just before this one.
micio
11-18-2003, 11:58 AM
I tried different ways and then I realized that every script is a subshell which has its own copy of shell variables, thus, changing it inside a script does not affect the parent shell!!
have a look at the code below: assignment is made properly but the change does not propagate to parent shell.
#!/bin/bash
PS3='please select a prompt here ==> '
echo
select test in "prompt1" "prompt2" "prompt3"
do
case $test in
prompt1) newprompt=$test;break ;;
prompt2) newprompt=$test;break ;;
prompt3) newprompt=$test;break;;
*) echo "error invalid $REPLY" ;;
esac
done
echo "prompt: $newprompt"
eval "PS1=$newprompt"
micio
Originally posted by micio
have a look at the code below: assignment is made properly but the change does not propagate to parent shell.
good point.
if you run the script this way it will work:
source scriptname
testPS3='please select a prompt here ==> '
echo
select test in "prompt1" "prompt2" "prompt3"
do
case $test in
prompt1) newprompt=$test;break ;;
prompt2) newprompt=$test;break ;;
prompt3) newprompt=$test;break;;
*) echo "error invalid $REPLY" ;;
esac
done
echo "prompt: $newprompt"
export PS1="$newprompt"
running the script:[ ddicks@linuxbox ~ ] $ source ./test
1) prompt1
2) prompt2
3) prompt3
please select a prompt here ==>
[ ddicks@linuxbox ~ ] $ . ./test
1) prompt1
2) prompt2
3) prompt3
please select a prompt here ==> 1
prompt: prompt1
prompt1
OR:
[ ddicks@linuxbox ~ ] $ source ./test
1) prompt1
2) prompt2
3) prompt3
please select a prompt here ==> 1
prompt: prompt1
prompt1
micio
11-18-2003, 01:38 PM
Originally posted by Hayl
if you run the script this way it will work:
source scriptname
great!! in fact, from bash man page:
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command exe-
cuted from filename.
in this way you do not create a sub shell.
micioc
supercoffee
11-19-2003, 07:30 AM
excellent!!! Thanks!