Ubuntu: What does ENDOFMENU do? [closed]



Question:

I have been given an example program, I am wondering what exactly the <<ENDOFMENU and ENDOFMENU does, won't it work the same if you leave it out and just use the while loop?

#!/bin/sh  echo "Menu test program...";  stop=0;  while test $stop -eq 0; do  cat<<ENDOFMENU  1: print the date  2,3 : print the current working directory  4: exit  ENDOFMENU    echo; echo -e "your choice?\c"    read reply  echo  case $reply in      "1")         date  ;;      "2"|"3")         pwd  ;;      "4")        stop=1  ;;      *)        echo illegal choice    esac  done  


Solution:1

ENDOFMENU as used in your example is a so-called here document or here doc. It allows for a multi-line string without needing to escape quoting characters such as ' and ".

Quoting the bash(1) manual:

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

<<[-]word  here-document  delimiter  

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

You showed the following code in your example:

cat<<ENDOFMENU  1: print the date  2,3 : print the current working directory  4: exit  ENDOFMENU    echo; echo -e "your choice?\c"  

That makes a string available as stream and then prints them to the console using the cat command. Finally, it prints another blank line followed by the string your choice? and an escape sequence that means "produce no further output" and effectively strips the newline that would follow. It could be rewritten as:

echo -e "    1: print the date  2,3 : print the current working directory  4: exit    your choice?\c"  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »