m4 adds extra space at top of file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting m4 adds extra space at top of file.
# 1  
Old 09-17-2010
m4 adds extra space at top of file.

I have used m4 in the past to generate source code where aesthetics and space were of no consequence [ provided it worked ]. Now I am using it to generate script and program templates [ in an effort to improve the readability and general soundness of my scripts/programs ].

So here is an excerpt from my m4 file for producing a generic bash script:

Code:
dnl `$Id$'
define(`START_SCRIPT',`#!/bin/bash
#####################################
# File: $1
# Auth: $2
# Desc:
#
# TODO:
# BUGS:
# RCS: $Id$
#####################################
')

It outputs what you'd expect but tacks on 16 blank lines at the beginning of the file. I can use sed to remove these but I am thinking there is an m4 basic I am missing. I've looked through the docs and various tutorials. Maybe I have a buggy m4?

Thanks!

Bubnoff

Last edited by Scott; 09-17-2010 at 03:07 PM.. Reason: Replaced ICODE tags with CODE tags
# 2  
Old 09-17-2010
Hi, I think you need to end your defines with dnlafter the closing parentheses
# 3  
Old 09-17-2010
This does help -- thanks. Where is this documented? I've been looking and haven't seen any examples where the defines were ended this way. I assume you mean:

Code:
define(`START_SCRIPT',`#!/bin/bash
#####################################
# File: $1
# Auth: $2
# Desc:
#
# TODO:
# BUGS:
# RCS: $Id$
#####################################
')dnl

Also, I still have 2 blank lines. Much less than what I had, but still producing blank lines.

What is the rule for this?

Thanks for your help!!!

Bub

Last edited by Scott; 09-21-2010 at 04:56 PM..
# 4  
Old 09-17-2010
Hi, I do not know where you can find it online.. FWIW, I checked my O'Reilly Sendmail book and it is in paragraph 19.1.2 (m4 and dnl).
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-17-2010
Thanks, I think I'm getting close -- down to one blank line.

I found references to this idiosyncrasy online.

Bub
# 6  
Old 09-17-2010
Code:
define(`START_SCRIPT',`#!/bin/bash
#####################################
# File: $1
# Auth: $2
# Desc:
#
# TODO:
# BUGS:
# RCS: $Id$
#####################################
')dnl 
START_SCRIPT()dnl

# 7  
Old 09-21-2010
Thanks for the replies. I tried adding dnl both in the macro and in the file and I still end up with one blank line at the top.

However, the resulting scripts work and if it really bothers me I can pull out a can o' sed on it.

What bothers me is the fact that there does not appear to be rhyme or reason for this behavior. Is m4 truly obsolete and no longer maintained? Other than sendmail is m4 worth the trouble verses using some bloated perl template script?

I hate to make a whing-fest out of a single blank line ...yet ...

Well, here's the pertinent parts for the curious. Maybe I've overlooked something else.

Sample use of macros:

Code:
include(`bash_macros.m4')
START_SCRIPT(`fake_script',`Automation Dept.')
GLOBALS()
SANITY_CHECKS()
FUNCTIONS()
MAIN()
END_SCRIPT()

Macros themselves:
Code:
define(`START_SCRIPT',#!/bin/bash
#####################################
# File: $1
# Auth: $2
# Desc:
#
# TODO:
# BUGS:
# RCS: $Id$
#####################################
)dnl
dnl
define(`LQ', `changequote(<,>)`dnl'
changequote`'')dnl
dnl
define(`RQ',`changequote(<,>)dnl`
'changequote`'')dnl
dnl
define(`GLOBALS',
#------------------------------------
# Globals
#------------------------------------
declare -rx SCRIPT=${0##*/}
SCRIPTNAME=${0%%.*}
FULLPATH=``$'0'
DATE=$`('date +%m-%d-%Y`)'
)dnl
dnl
define(`SANITY_CHECKS',
#------------------------------------
# Sanity Checks
#------------------------------------
if test -z "$BASH" ; then
    printf "$SCRIPT:$LINENO: Use BASH to run this script.\n" >&2
    exit 192
fi

# Test binaries
# Example:
# if test ! -x "$PRINT" ; then
#     printf "$SCRIPT:$LINENO: the command $PRINT is not available." >&2
#     exit 192
# fi

# Test file existence
# if test ! -e "$BKPLOG" ; then
#        RPT="File not found"
# else
#        RPT=$(/usr/bin/basename $BKPLOG)
# fi
)dnl
dnl
define(`FUNCTIONS',
#------------------------------------
# Functions
#------------------------------------
function func_usage ()
{
    echo ""
    echo "USAGE: "
    echo "    script_template.sh [-fh] <filename>"
    echo ""
    echo "OPTIONS:"
    echo "    -f  <filename>"
    echo "    -h  usage info"
    echo ""
    echo "EXAMPLE:"
    echo "    script_template.sh -f <filename>"
    echo ""
    exit $E_OPTERROR    # Exit and explain usage, if no argument(s) given.
}
)dnl
dnl
define(`MAIN',
#------------------------------------
# MAIN
#------------------------------------

# process args.
while getopts ":f:h" option
do
    case $option in
        f    `)' FILE=$OPTARG
             ;;
        h    `)' func_usage
               exit 0
             ;;
        *    `)' echo ""
               echo "Bad option."
               func_usage
    esac
done

shift $(($OPTIND - 1))
)dnl
dnl
define(`END_SCRIPT',
#---------------------------
# Cleanup
#---------------------------
exit 0
)dnl

A sample run:

Code:
m4 test.txt > new_script.sh

You will get a fairly textbook bash script template ...overkill for many tasks, but nice for larger stuff ...with a blank line at the top.

I am using m4 version 1.4.13 on Ubu 10.04.

Thanks for reading!

Bubnoff
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extra Space in output - remove

Hi All I am trying to perform the below operation -count=`cat abc.txt | wc -l` echo$count 5 Head=Start"$DATE"00000"$count"File echo $HEAD START15020300000 5File There is a space coming before 5 which is not needed . How to ignore that . (4 Replies)
Discussion started by: honey26
4 Replies

2. Shell Programming and Scripting

Space usage by top 5 users in a filesystem

I want to see top 5 users,who have occupied most amount of disk space in a filesystem. But not sure how to do it. I can get the usage for a particular user find . -user user -type f exec df -h {} \;|awk '{ s = s+$1 } END { print "Total used: ",s }' But how to get without specifying any user... (6 Replies)
Discussion started by: rka
6 Replies

3. UNIX for Dummies Questions & Answers

#Spool/Query executing with extra space.

Hello, I have a requirement where i have to spool some data to a file. i have achived the desired target but m facing one issue. i have attached the script and the output. i checked the data length in the table but it is only 45 for column 1. can you tell me how to remove these extra... (4 Replies)
Discussion started by: Mohammed_Tabish
4 Replies

4. Shell Programming and Scripting

[Solved] Howto remove extra space in the file

Hi Gurus, I have a file which contains some special char or space. when using cat -evt I can see the file as following: 0,"0000","abc/def aaa ... (6 Replies)
Discussion started by: ken6503
6 Replies

5. Shell Programming and Scripting

my shell now adds extra space at end of each line!

Hi, Since today, with csh or tcsh, if I do 'ls files* > list', every lines end with an extra space! What happenned? What can I do to go back when there was no extra space? If I change to bash, there's no extra space. Thanks, Patrick ---------- Post updated at 03:19 PM... (1 Reply)
Discussion started by: trogne
1 Replies

6. Solaris

Which gives the correct swap space top/vmstat ?

last pid: 29502; load avg: 21.8, 20.7, 20.4; up 8+08:49:09 763 processes: 589 sleeping, 9 running, 160 zombie, 5 on cpu CPU states: 0.0% idle, 28.2% user, 71.8% kernel, 0.0% iowait, 0.0% swap Memory: 32G phys mem, 2042M free mem, 8198M total swap, 8020M free swap kthr ... (2 Replies)
Discussion started by: chidori
2 Replies

7. Shell Programming and Scripting

extra space issue with awk

for diskname in $(lspv |awk '{print $1}') do lquerypv -h /dev/|awk '/'$diskname'/ { print ; exit }' done No output is returning from the loop. I think awk put an extra space to the command - lquerypv -h /dev/ so that the command is executed as i.e. lquerypv -h /dev/ hdisk230 with a space... (7 Replies)
Discussion started by: Daniel Gate
7 Replies

8. Shell Programming and Scripting

Script to check top 5 biggest disk space users

Hi all, I am needing a bash shell script to generate a list of the top 5 users using the most disk space. I am thinking that the du command would be used somehow but I am at a loss. Can anyone help? Thanks! (3 Replies)
Discussion started by: sytemx
3 Replies

9. AIX

How to find the top 6 users (which consume most space)?

Hi everybody, I want to know if there is any posibility to find out - on an AIX system - which are the the users who consume most space or at least a posibility to obtain a list with all the users and how much space are they consuming ? Trying to use du command was useless. Any idea?... (5 Replies)
Discussion started by: RebelDac
5 Replies

10. UNIX for Dummies Questions & Answers

Swap space used greater than allocated using top

Hi there, When I run top on my machine it says I have 497M swap space in use, and 380M swap space free, but I have only allocated 512M swap space to the machine!!!! Does anyone know how swap used is calculated in the top command? Thanks... (1 Reply)
Discussion started by: chorgan
1 Replies
Login or Register to Ask a Question