LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [bash] code wont work - here document (https://www.linuxquestions.org/questions/programming-9/%5Bbash%5D-code-wont-work-here-document-747742/)

RaptorX 08-15-2009 07:30 AM

[bash] code wont work - here document
 
Hi guys,

reading advanced bash scripting, I found this example:
Code:

#!/bin/bash
# self-document.sh: self-documenting script
# Modification of "colm.sh".

DOC_REQUEST=70

if [ "$1" = "-h"  -o "$1" = "--help" ]    # Request help.
then
  echo; echo "Usage: $0 [directory-name]"; echo
  sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |
  sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi


: <<DOCUMENTATIONXX
List the statistics of a specified directory in tabular format.
---------------------------------------------------------------
The command-line parameter gives the directory to be listed.
If no directory specified or directory specified cannot be read,
then list the current working directory.

DOCUMENTATIONXX

if [ -z "$1" -o ! -r "$1" ]
then
  directory=.
else
  directory="$1"
fi 

echo "Listing of "$directory":"; echo
(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
; ls -l "$directory" | sed 1d) | column -t

exit 0

I made my own based on this one and it didnt work, now that i copy pasted this one and tried it also doesnt work.

Can you tell me why?

actually if you paste this in to bash:

Code:

echo "Listing of "$directory":"; echo
(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
; ls -l "$directory" | sed 1d) | column -t

it works fine, but when i run test.sh (which contains the whole code) it simply gives me the $ again without doing anything.

Any info would be appreciated, since the code looks logically correct.

catkin 08-15-2009 07:44 AM

Works for me (with columns misaligned because of different ls -l output).

Does it run at all? Try
Code:

#!/bin/bash
set -xv
<rest of script>

or
Code:

#!/bin/bash
echo 'Hello!'
<rest of script>


RaptorX 08-15-2009 07:45 AM

after running test.sh

I get

Code:

[~] echo $?
1

so it is not working, the thing is that i do not understand why...

RaptorX 08-15-2009 07:50 AM

Quote:

Originally Posted by catkin (Post 3644311)
Works for me (with columns misaligned because of different ls -l output).

Does it run at all? Try
Code:

#!/bin/bash
set -xv
<rest of script>

or
Code:

#!/bin/bash
echo 'Hello!'
<rest of script>


same results

tommylovell 08-15-2009 09:15 AM

When you added the "set -xv" to the script and reran it, did it still just produce the prompt and no output?

Mine produced:
Code:

[tommy@athlonz ~]$ ./test.sh
# self-document.sh: self-documenting script
# Modification of "colm.sh".

DOC_REQUEST=70
+ DOC_REQUEST=70

if [ "$1" = "-h"  -o "$1" = "--help" ]    # Request help.
then
  echo; echo "Usage: $0 [directory-name]"; echo
  sed --silent -e '/DOCUMENTATIONXX$/,/^DOCUMENTATIONXX$/p' "$0" |
  sed -e '/DOCUMENTATIONXX$/d'; exit $DOC_REQUEST; fi
+ '[' '' = -h -o '' = --help ']'


: <<DOCUMENTATIONXX
List the statistics of a specified directory in tabular format.
---------------------------------------------------------------
The command-line parameter gives the directory to be listed.
If no directory specified or directory specified cannot be read,
then list the current working directory.

DOCUMENTATIONXX
+ :

if [ -z "$1" -o ! -r "$1" ]
then
  directory=.
else
  directory="$1"
fi 
+ '[' -z '' -o '!' -r '' ']'
+ directory=.

echo "Listing of "$directory":"; echo
+ echo 'Listing of .:'
Listing of .:
+ echo

(printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
; ls -l "$directory" | sed 1d) | column -t
+ printf 'PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n'
+ sed 1d
+ ls -l .
+ column -t
PERMISSIONS  LINKS  OWNER  GROUP  SIZE        MONTH      DAY    HH:MM          PROG-NAME
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Desktop
drwxr-xr-x  6      tommy  tommy  4096        2009-05-08  09:50  Documents
drwxr-xr-x  3      tommy  tommy  4096        2009-04-10  11:16  Download
drwxr-xr-x  2      root  root  4096        2009-03-11  22:58  getdev
drwxr-xr-x  2      root  root  4096        2009-05-10  20:20  lost
drwxr-xr-x  2      root  root  4096        2009-05-08  00:01  malloctest
-rw-r--r--  1      root  root  1183081233  2009-06-15  17:09  mem_stats.txt
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Music
drwx------  3      tommy  tommy  4096        2009-04-05  00:23  Network        Trash      Folder
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Pictures
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Public
-rwxr-xr-x  1      root  root  1440        2009-03-11  23:06  set_authkey.sh
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Templates
drwx------  3      tommy  tommy  4096        2009-04-05  00:23  Temporary      Items
-rwxrwxr-x  1      tommy  tommy  917        2009-08-15  10:13  test.sh
drwxr-xr-x  2      tommy  tommy  4096        2009-03-24  01:06  Videos

exit 0
+ exit 0
[tommy@athlonz ~]$

If it produced any output, please cut-and-paste it.

PTrenholme 08-15-2009 12:38 PM

Not an answer to your question, but a common alternative to your example is to use the cat command rather than the sed command. With that change, your sample code would look like this:
PHP Code:

#!/bin/bash
# self-document.sh: self-documenting script
# Modification of "colm.sh".

DOC_REQUEST=70

if [ "$1" "-h"  -"$1" "--help" ]     # Request help.
then
  
echo; echo "Usage: $0 [directory-name]"; echo
cat <<DOCUMENTATIONXX
List the statistics of a specified directory in tabular format.
---------------------------------------------------------------
The command-line parameter gives the directory to be listed.
If 
no directory specified or directory specified cannot be read,
then list the current working directory.

DOCUMENTATIONXX
exit $DOC_REQUEST
fi

if [ -"$1" -! -"$1" ]
then
  directory
=.
else
  
directory="$1"
fi  

echo "Listing of "$directory":"; echo
(
printf "PERMISSIONS LINKS OWNER GROUP SIZE MONTH DAY HH:MM PROG-NAME\n" \
ls -"$directorysed 1d) | column -t

exit 


RaptorX 08-17-2009 12:02 PM

@tommylovell

Everything went exactly the same... no output from the script and a new line with a prompt.

I was noticing that when i pasted the code all the expanded variables where missing ("$1" was showing up like "".). I do not know why, after I inserted all variables everything worked as you mentioned.


@PTrenholme

Thanks for the idea, I tried it and it works perfectly!


All times are GMT -5. The time now is 09:45 PM.