LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-15-2009, 07:30 AM   #1
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Rep: Reputation: 37
[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.
 
Old 08-15-2009, 07:44 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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>
 
Old 08-15-2009, 07:45 AM   #3
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
after running test.sh

I get

Code:
[~] echo $?
1
so it is not working, the thing is that i do not understand why...
 
Old 08-15-2009, 07:50 AM   #4
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by catkin View Post
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
 
Old 08-15-2009, 09:15 AM   #5
tommylovell
Member
 
Registered: Nov 2005
Distribution: Fedora, Redhat
Posts: 372

Rep: Reputation: 101Reputation: 101
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.
 
Old 08-15-2009, 12:38 PM   #6
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
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 -"$directory" sed 1d) | column -t

exit 
 
Old 08-17-2009, 12:02 PM   #7
RaptorX
Member
 
Registered: Jun 2009
Location: Emden, Germany
Distribution: Slackware 12.2, Slax 6.1
Posts: 254

Original Poster
Rep: Reputation: 37
@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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a broken bash document? ninja master Linux From Scratch 10 01-29-2009 02:54 PM
how to make my code to work on bsd (pptp script on bash) jabka *BSD 1 05-01-2007 12:53 PM
Any document to understand e1000 network device driver source code ahm_irf Linux - Networking 1 02-14-2007 01:37 AM
Document required about Fedora source code ahm_irf Fedora 1 02-11-2007 10:16 PM
apache wont accept document root to be in a mounted fat partion a1ex_007 Red Hat 1 04-09-2005 06:34 PM


All times are GMT -5. The time now is 08:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration