0

I'm currently at /opt and running a small shell script. My "no" condition works perfectly, however, my yes condition doesn't work, I 'am still at /opt.

while true; do
    read -p "go to log location ?" yn
    case $yn in
        [Yy]* ) cd /var/tmp/logs; break;;
        [Nn]* ) echo "you are @ : " $PWD; break;;
        * ) echo "Please answer yes or no.";;
    esac
done

Any help ??

  • @Kusalananda could you pls. undo yout edit as his problem lays in using the 'script' in the command line. – bey0nd May 09 '20 at 22:38
  • @bey0nd I only formatted his code as a code block. The code shown currently is his original code, all indented 4 spaces. – Kusalananda May 09 '20 at 22:41
  • Why was this question closed? The problem was not how to change the directory, but a coding error and therefor doesn't have anything to do with the referred link. – bey0nd May 09 '20 at 22:41
  • @bey0nd No syntax error in the original code, check the revisions of the question. – Kusalananda May 09 '20 at 22:42
  • That is not correct. In the original code there is no ';' between 'yn' and 'case' which leads to a not working "script" as commands on the command line have to be separated with a ';' or a line brake as with your edit. Just copy&paste the original code and your revision into a shell and you see the difference. – bey0nd May 09 '20 at 22:45
  • 1
    @bey0nd The original code, if you check "side-by-side markdown" in the revisions, has proper indentation. I just turned it into a marked-up code block, literally only inserting four spaces in front of each line. – Kusalananda May 09 '20 at 22:46
  • I 'am not sure, what this, how do I fix my issue ?? – Pragnya Kolagotla May 09 '20 at 22:47
  • @PragnyaKolagotla Please see the question marked as a duplicate, the accepted answer lets you know that your script would work if you sourced it with either . or source. Another answer suggest creating a shell function for the task (which is what I would have done). – Kusalananda May 09 '20 at 22:49
  • @Pragnya Kolagotla if I'm right and you want to execute these commands directly on the command line and not in a script or source it from a file, then just use while true; do read -p "go to log location ?" yn; case $yn in [Yy]* ) cd /var/tmp/logs; break;; [Nn]* ) echo "you are @ : " $PWD; break;; * ) echo "Please answer yes or no.";; esac done – bey0nd May 09 '20 at 22:54

1 Answers1

-1

You are missing a ; behind read -p "go to log location ?" yn

bey0nd
  • 937
  • 1
    ; not needed at the end of a line. The issue is expecting cd to affect the shell that called the script. See duplicate question. – Kusalananda May 09 '20 at 22:38
  • In his one-liner there was neither a "end of line" or a ';' behind the read -p "go to log location ?" yn command! – bey0nd May 09 '20 at 22:51
  • 1
    It was not a one-liner. The code that looked as if it was all on one line was all on one line because it was not marked up as code in the question. The question text lacked the code block indentation (4 spaces at the start of the line). I added this indentation, and nothing else, to properly mark up the code. In doing so, I never changed the syntax, never added nor removed newlines or changed the existing visual indentation as it now appears in the question. – Kusalananda May 09 '20 at 22:54
  • @Kusalananda I see what you mean. With the missing "#!/bin/..." at the beginning and everything in one line, I just took it as a one-liner and not as a snippet out of a script. And it also worked perfectly as a one-liner with that additional ';'. Even the change of directory was persistent. To bad. ;-) – bey0nd May 09 '20 at 23:25
  • Yeah, but notice that the OP said ‘‘My "no" condition works perfectly’’.  If they had been doing the entire script as a one-liner (as you interpreted it), it would never have gotten as far as asking the yes or no question. – G-Man Says 'Reinstate Monica' May 09 '20 at 23:39
  • @beyond I reverted the question to its original and added just backticks to make the code a "block of code", There were spaces and newlines there, it was not a "one-liner". Just a data point :-). Hope this Helps. –  May 10 '20 at 00:50
  • @G-ManSays'ReinstateMonica' I noticed the comment about the "no" condition. Nevertheless, when I copied&pasted the provided code, which appeared before any editing as one line, into a shell it did behave just as described and with the additional ';' it worked perfectly. I understand now that, by the way this web site presented the code segment in one line, I was tricked to believe it was one line. – bey0nd May 10 '20 at 09:10
  • Well, I can’t help thinking that there’s something peculiar going on with your shell, because there’s no way … read -p "go to log location ?" yn case $yn in [Yy]* ) cd /var/tmp/logs … should work. – G-Man Says 'Reinstate Monica' May 10 '20 at 09:42
  • @Isaac: I don’t understand what your objective was, or what you think you accomplished.  It appears to me that all you did was roll back Kusalananda’s edit and then re-make it, using slightly different Markdown. I assume that you know not to make trivial, cosmetic edits to recently-closed questions. – G-Man Says 'Reinstate Monica' May 10 '20 at 09:50
  • I think my shell is just fine. :-) I rather think that is has something to do with how the web page presented that code in on line. As Kusalananda and Isaac stated, they just marked the code as code and the formatting appeared. Therefore it must also have been includet in the single line presented before editing and was copied with the c&p action into the shell. – bey0nd May 10 '20 at 09:51
  • @G-ManSays'ReinstateMonica' Well, my objective was to make it perfectly clear that the newlines were there before Kusalananda edited the question. The user bey0nd has repeatedly claimed that the initial OP had no newlines. It is a problem of this site in that it collapses spaces and newlines everywhere (yes, I know, is part of HTTP), that might confuse people, and, in this case it is confusing bey0nd, Sorry, forgot that editing a "just closed" question affects its state. Overall, it seems that no big harm was done and that bey0nd got some help. Should anything else be done? –  May 10 '20 at 18:35
  • @Isaac:  My thought is that either bey0nd understands how to read a post’s revision history, or they don’t.  If they do, then they agreed with Kusalananda’s point before you made your edits.  (And their “I see what you mean.” comment, 75 minutes before you made your edits, suggests that this is the case.)  And if they don’t understand how to read a revision history, then your edits (which made no *net* visible change), didn’t help. … … … … … … … … … … … … … … … “Should anything else be done?”  I can’t think of anything. – G-Man Says 'Reinstate Monica' May 10 '20 at 23:46
  • G-ManSays'ReinstateMonica' (1) If nothing needs to be done: Why the insistence? (2) You seem to have already reached a conclusion: no net visible change. (Cont...) –  May 11 '20 at 06:25
  • @G-ManSays'ReinstateMonica' (3) I disagree. What I see, if I open the revision history and select the side-by-side markdown, is that the bottom entry (Number 1) contains the code all collapsed into one-line. Then, as number 2, there is the Kusalananda edit, that shows some green additions visually shown as 4 spaces, but a newline could have been added as well and it will be included in the green area. Then the revision number 4, the top one, My edit, shows only backticks (well a bash word as well) in green, showing clearly that that is the only addition, newlines were already there. (Cont..) –  May 11 '20 at 06:28
  • (Cont...) @G-ManSays'ReinstateMonica' If you are unable to see those visual clues then, as you said: My thought is that either you* understand how to read a post’s revision history, or you don’t.* (4) If what you want is to help bey0nd, then you could tell him to open the edit window of the first post where he would see that the OP included the conflicting newlines. (5) If what you want is to show a mistake that you believe I committed, I'll be so bold as to say: it is probably you who is mistaken. –  May 11 '20 at 06:29
  • @G-ManSays'ReinstateMonica' (6) This comment about the need to add a ; was long (22 minutes) after Kusalananda edited the question. He still needed some help. –  May 11 '20 at 06:34
  • 1
    Folks, it seems I saw the original post while Kusalananda was editing it. When I saw it, I thought, based of a missing '#!/bin/bash', that it was meant to be a one-liner (my mistake) and c&p'ed the code. I couldn't reproduce it again, but the first time the 'script' partly worked and I got to the "no" part of it. By adding the ';' it completely worked in the shell. I wasn't familiar with how to revise the edits of a post (my lesson learned ;-) ) but can see now what Isaac wanted to show me about the NewLines. Nevertheless, I think its about time to put a lid on it an go on with life. – bey0nd May 11 '20 at 07:29