How to pass a value to read input before being prompted?Change Gsetting with script on LogoutPass variable value to script from terminalPass Variable Value as Argument to CommandRead input for bash scriptexit a bash command in a script without exiting the scriptCommand substitution doesn't work when it's the input to `read`pass return value from firefox process to linux shell?

How is warfare affected when armor has (temporarily) outpaced guns? How can guns compete?

How to initiate a conversation with a person who recently had transition but you were not in touch with them?

Continents with simplex noise

Are the Properties of the EM Spectrum Fluid?

What to do with developers who don't follow requirements?

Right way to say I disagree with the design but ok I will do

Radar Altimeter in Space Shuttle

How did the T-850 still function after it removed its second battery?

Where do overtones in a 555 generated square wave come from?

Hell0 W0rld! scored by ASCII values

Who originated the dangerous avocado-pitting technique?

What are some examples of three-mora atamadaka verbs besides 帰る?

indent and noindent: details from Knuth's The TeXbook

What would be the best propulsion system for this aircraft carrier?

Are there any real life instances of aircraft aborting a landing to avoid a vehicle?

Alternatives to boxes

How offensive is Fachidiot?

Why would Basel III prevent price discovery at credit markets?

What does a single quote inside a C# date time format mean?

Non-differentiable Lipschitz functions

Why can't I book this multi-city fare on American Airlines?

Run "cd" command as superuser in Linux

Meaning of 'off one's brake fluid'

Why don't my appliances work when my tester shows voltage at the outlets?



How to pass a value to read input before being prompted?


Change Gsetting with script on LogoutPass variable value to script from terminalPass Variable Value as Argument to CommandRead input for bash scriptexit a bash command in a script without exiting the scriptCommand substitution doesn't work when it's the input to `read`pass return value from firefox process to linux shell?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0

















I have shell script prompting an answer y/n. at the prompt before giving input, I used control-c signal which calls Signal Handling function. In Signal Handling function there is a prompt "q" to exit or "y" and "y" should be used with the FIRST read prompt.



I tried to (( echo "y" | read )) but didn't work



This is part of my script:



IntHandle ()

echo -e "nUse 'q' to quit "
read var1
if [[ $var1 == q ]]
then
exit 1
else
echo "y"


trap 'IntHandle' SIGINT
read -p "no valid user id entered, new user ids? [yn]: " ans ----> here
used control-c signal before give y/n to ans
if [[ $ans == "y" ]]
then
read -p " username :" name
fi
.
.
.
.


.



output should be like below:



no valid user id entered, new user ids? [yn]: #control-c entered



' Use 'q' to quit ' y ------> here "y" entered rather "q" in Siganl Handeling
function then it is saved in "ans" variable
which gets the condition true to prompt a username.



usernames: Larry -----> the name which is entered after true condition. . . .










share|improve this question


































    0

















    I have shell script prompting an answer y/n. at the prompt before giving input, I used control-c signal which calls Signal Handling function. In Signal Handling function there is a prompt "q" to exit or "y" and "y" should be used with the FIRST read prompt.



    I tried to (( echo "y" | read )) but didn't work



    This is part of my script:



    IntHandle ()

    echo -e "nUse 'q' to quit "
    read var1
    if [[ $var1 == q ]]
    then
    exit 1
    else
    echo "y"


    trap 'IntHandle' SIGINT
    read -p "no valid user id entered, new user ids? [yn]: " ans ----> here
    used control-c signal before give y/n to ans
    if [[ $ans == "y" ]]
    then
    read -p " username :" name
    fi
    .
    .
    .
    .


    .



    output should be like below:



    no valid user id entered, new user ids? [yn]: #control-c entered



    ' Use 'q' to quit ' y ------> here "y" entered rather "q" in Siganl Handeling
    function then it is saved in "ans" variable
    which gets the condition true to prompt a username.



    usernames: Larry -----> the name which is entered after true condition. . . .










    share|improve this question






























      0












      0








      0








      I have shell script prompting an answer y/n. at the prompt before giving input, I used control-c signal which calls Signal Handling function. In Signal Handling function there is a prompt "q" to exit or "y" and "y" should be used with the FIRST read prompt.



      I tried to (( echo "y" | read )) but didn't work



      This is part of my script:



      IntHandle ()

      echo -e "nUse 'q' to quit "
      read var1
      if [[ $var1 == q ]]
      then
      exit 1
      else
      echo "y"


      trap 'IntHandle' SIGINT
      read -p "no valid user id entered, new user ids? [yn]: " ans ----> here
      used control-c signal before give y/n to ans
      if [[ $ans == "y" ]]
      then
      read -p " username :" name
      fi
      .
      .
      .
      .


      .



      output should be like below:



      no valid user id entered, new user ids? [yn]: #control-c entered



      ' Use 'q' to quit ' y ------> here "y" entered rather "q" in Siganl Handeling
      function then it is saved in "ans" variable
      which gets the condition true to prompt a username.



      usernames: Larry -----> the name which is entered after true condition. . . .










      share|improve this question

















      I have shell script prompting an answer y/n. at the prompt before giving input, I used control-c signal which calls Signal Handling function. In Signal Handling function there is a prompt "q" to exit or "y" and "y" should be used with the FIRST read prompt.



      I tried to (( echo "y" | read )) but didn't work



      This is part of my script:



      IntHandle ()

      echo -e "nUse 'q' to quit "
      read var1
      if [[ $var1 == q ]]
      then
      exit 1
      else
      echo "y"


      trap 'IntHandle' SIGINT
      read -p "no valid user id entered, new user ids? [yn]: " ans ----> here
      used control-c signal before give y/n to ans
      if [[ $ans == "y" ]]
      then
      read -p " username :" name
      fi
      .
      .
      .
      .


      .



      output should be like below:



      no valid user id entered, new user ids? [yn]: #control-c entered



      ' Use 'q' to quit ' y ------> here "y" entered rather "q" in Siganl Handeling
      function then it is saved in "ans" variable
      which gets the condition true to prompt a username.



      usernames: Larry -----> the name which is entered after true condition. . . .







      command-line bash






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 8 at 16:44









      vidarlo

      13.5k6 gold badges33 silver badges59 bronze badges




      13.5k6 gold badges33 silver badges59 bronze badges










      asked Jun 8 at 16:25









      RedHatRedHat

      1




      1























          1 Answer
          1






          active

          oldest

          votes


















          2


















          This is a X-Y problem. Why on earth use read for setting a variable in a script?



          Set it the normal way; ans=y



          This would make the function look like this:



          IntHandle ()

          echo -e "nUse 'q' to quit "
          read var1
          if [[ $var1 == q ]]
          then
          exit 1
          else
          ans=y
          fi






          share|improve this answer


























          • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

            – RedHat
            Jun 9 at 0:58












          • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

            – RedHat
            Jun 9 at 1:00











          • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

            – vidarlo
            Jun 9 at 7:25













          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );














          draft saved

          draft discarded
















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1149621%2fhow-to-pass-a-value-to-read-input-before-being-prompted%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown


























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2


















          This is a X-Y problem. Why on earth use read for setting a variable in a script?



          Set it the normal way; ans=y



          This would make the function look like this:



          IntHandle ()

          echo -e "nUse 'q' to quit "
          read var1
          if [[ $var1 == q ]]
          then
          exit 1
          else
          ans=y
          fi






          share|improve this answer


























          • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

            – RedHat
            Jun 9 at 0:58












          • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

            – RedHat
            Jun 9 at 1:00











          • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

            – vidarlo
            Jun 9 at 7:25
















          2


















          This is a X-Y problem. Why on earth use read for setting a variable in a script?



          Set it the normal way; ans=y



          This would make the function look like this:



          IntHandle ()

          echo -e "nUse 'q' to quit "
          read var1
          if [[ $var1 == q ]]
          then
          exit 1
          else
          ans=y
          fi






          share|improve this answer


























          • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

            – RedHat
            Jun 9 at 0:58












          • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

            – RedHat
            Jun 9 at 1:00











          • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

            – vidarlo
            Jun 9 at 7:25














          2














          2










          2









          This is a X-Y problem. Why on earth use read for setting a variable in a script?



          Set it the normal way; ans=y



          This would make the function look like this:



          IntHandle ()

          echo -e "nUse 'q' to quit "
          read var1
          if [[ $var1 == q ]]
          then
          exit 1
          else
          ans=y
          fi






          share|improve this answer














          This is a X-Y problem. Why on earth use read for setting a variable in a script?



          Set it the normal way; ans=y



          This would make the function look like this:



          IntHandle ()

          echo -e "nUse 'q' to quit "
          read var1
          if [[ $var1 == q ]]
          then
          exit 1
          else
          ans=y
          fi







          share|improve this answer













          share|improve this answer




          share|improve this answer










          answered Jun 8 at 16:43









          vidarlovidarlo

          13.5k6 gold badges33 silver badges59 bronze badges




          13.5k6 gold badges33 silver badges59 bronze badges















          • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

            – RedHat
            Jun 9 at 0:58












          • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

            – RedHat
            Jun 9 at 1:00











          • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

            – vidarlo
            Jun 9 at 7:25


















          • First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

            – RedHat
            Jun 9 at 0:58












          • I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

            – RedHat
            Jun 9 at 1:00











          • No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

            – vidarlo
            Jun 9 at 7:25

















          First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

          – RedHat
          Jun 9 at 0:58






          First, I did before "ans=y" but did not work because I am still being prompted to enter "y" or "n" in the first prompt which it should not. Just when I enter "y" rather than "q" in the function prompt, it would set "ans" variable by "y" and skip me the prompt.

          – RedHat
          Jun 9 at 0:58














          I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

          – RedHat
          Jun 9 at 1:00





          I need to skip the stdin in the first prompt when I use control-c signal before giving "y" or "n" to ans variable.

          – RedHat
          Jun 9 at 1:00













          No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

          – vidarlo
          Jun 9 at 7:25






          No, you need to modify the flow of the program to skip that prompt if the value is set. Whenever ^C is pressed, your code will jump to IntHandle(). How can that be used to skip the prompt? I'm trying to nudge you along here, because, as I said, you have put your eyes on a solution, which is wrong.

          – vidarlo
          Jun 9 at 7:25



















          draft saved

          draft discarded















































          Thanks for contributing an answer to Ask Ubuntu!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1149621%2fhow-to-pass-a-value-to-read-input-before-being-prompted%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown









          Popular posts from this blog

          Tamil (spriik) Luke uk diar | Nawigatjuun

          Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

          Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?