Find the answer to your Linux question:
Results 1 to 8 of 8
I keep hitting a problem on a Linux system where I'm running a certain shell script, it prompts for input, and I can't type anything. I've finally been able to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66

    Question puzzling behavior after command editing


    I keep hitting a problem on a Linux system where I'm running a certain shell script, it prompts for input, and I can't type anything. I've finally been able to reproduce my steps to get this to happen in a small script but I'm not sure if there's much I can do about the problem aside from not causing it to happen in the first place. Here's the script in question:

    Code:
    trap "echo 'quitter!!'; exit" INT
    
    ans=""
    while [ "X$ans" != Xcontinue ]
    do
      echo "Enter 'continue' to continue..."
      read ans
    done
    echo "Good job"
    Looks pretty harmless, right? Here's what I do to execute it that can get me into trouble:

    1. Enable vi command editing (set -o vi)
    2. Type the letter v to launch vi on a temporary file in which I can enter a command to execute
    3. Type a command to execute the script. If the script is called prompter, I might just add ./prompter to the file
    4. Type the letters :wq to end the vi session and execute the command


    At this point, some flavors of Linux let me type input for the script but some do not. I haven't tried it on a lot of different systems but two systems I have handy (one is CentOS and the other is SuSE) do not allow me to type input. I also have two others (Redhat and Ubuntu) which gladly accept the input.

    Regardless of whether the system lets me type input to the script or how I end it, all of the systems leave me in command editing mode after the script completes. I have to hit Enter or use the vi commands a or i to type anything.

    Has anyone else seen this, am I doing something blindingly stupid?

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    652
    Enable vi command editing (set -o vi)
    This does not mean that you are in vi when you enter commands. What this does is use vi commands while entering the command. For example you have typed but not entered the line:
    Code:
    vim /tmp/my_junk
    and you realize the you forgot the option "+/fred" before the file. To enter vi editing mode you hit the "escape" key. Then type "B" to backup to the start of the "white space delimited word (in this case you will be on the leading "/" of the string "/tmp/my_junk". Type the "i" key (to enter insert mode) and type "+/fred " and at this point you can hit "Enter" to submit the command.

    Note: you could type "bbbb" rather than "B" or even "4b" rather than "B". Vi command such as "O" or "o" do not work a a command is a single line. When in vi-editing mode, you can hit the "Escape" key and then "j', 'k', up-arrow, down-arrow to move through you command history and be in editing mode on that line. If you fail to hit the "Escape" key and use the arrow keys to access your history, you will not in vi-editing mode on the line until you do hit the "Escape" key. As a visual, if you have not moved the cursor horizontally, the cursor will be at the start of the line when in vi-editing mode and at the end of the line when not in vi-editing mode.

    When you are in vi (by typing v) you are typing into a file that has not yet been named and not the command line buffer.
    Last edited by alf55; 01-18-2013 at 04:41 AM. Reason: Corrected "Escape" spelling

  3. #3
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Thanks for your reply but I think you may have missed the entire point of my post. Perhaps it's my mistake for not being more clear.

  4. #4
    Linux Newbie mactruck's Avatar
    Join Date
    Apr 2012
    Location
    City of Salt
    Posts
    187
    when you run the script on the computers that don't work what happens? It looks like it just runs and does nothing?

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    652
    Quote Originally Posted by mrbruno View Post
    Thanks for your reply but I think you may have missed the entire point of my post. Perhaps it's my mistake for not being more clear.
    What is the point? "vi command editing" does not use the vi editor. It just changes the the editing in the "readline" within the shell.

    So what are you doing?


    Normally v is not mapped to loading the editor.

  6. #6
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Quote Originally Posted by mactruck View Post
    when you run the script on the computers that don't work what happens? It looks like it just runs and does nothing?
    When the script prompts me to continue, it doesn't accept any normal keyboard input. It doesn't any characters I type, it doesn't respond when I press ENTER, etc. The only thing I've found that it responds to is ctrl-C and that's only because I set up the handler. I think before I added the handler, it ignored ctrl-C as well.

  7. #7
    Just Joined! mrbruno's Avatar
    Join Date
    Jan 2013
    Location
    /MilkyWay/Sol/Earth/USA/NC/Raleigh
    Posts
    66
    Quote Originally Posted by alf55 View Post
    What is the point? "vi command editing" does not use the vi editor. It just changes the the editing in the "readline" within the shell.

    So what are you doing?
    What I'm doing is complex command line editing in a safer area than directly at the command line. I don't know about you, but I sometimes make mistakes at the command line and cause the command to be executed before I'm done doctoring it up. If I use vi to do the editing, I have to make a more explicit action to execute the command.

    Quote Originally Posted by alf55 View Post
    Normally v is not mapped to loading the editor.
    I disagree with you. If you enable vi command editing, press ESC to enter command mode, and press the v key, the shell launches vi on the contents of the command line. You make your changes, "save it", and the altered contents get executed. It's an extremely useful feature I use every day.

  8. #8
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    setting and exporting EDITOR to /usr/bin/vim into ~/.bashrc (sourcing it), and typing C-x C-e will allow you to quickly access vim editor, without the confusing behaviour of bash's vi-mode.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •