LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 04-21-2004, 02:59 PM   #1
vsop
LQ Newbie
 
Registered: Apr 2004
Location: MD
Posts: 5

Rep: Reputation: 0
Question I'm lost - bash script


I need to right a script for this class that prints out the arguments from the command line each on a seperate line diagonaly.

So that each word on the next line starts where the last left off with no spaces.

so if the arguments were AAA BBBB CCCC DDDD EEEEE FFFF
it would print out

AAA
===BBBB
=======CCCC
===========DDDD
===============EEEEE
===================FFFF

(I just used the '=' where blank spaces should be
I can't figure out how to go about getting the spacing for each argument

Last edited by vsop; 04-21-2004 at 03:02 PM.
 
Old 04-21-2004, 03:18 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 58
What about:
Code:
#!/bin/bash

IFS="
"
count=0;
for line in `cat infile`;do
 for ((num=0;$count != $num;num=$(($num+1))));do
  echo -n " "
 done
 echo $line
 num=`echo $line | wc -m`
 count=$(($count+$num))
done
 
Old 04-21-2004, 03:31 PM   #3
vsop
LQ Newbie
 
Registered: Apr 2004
Location: MD
Posts: 5

Original Poster
Rep: Reputation: 0
Thats close, I need to get rid of the space between the begining of the word on the next line and the end of the word on the line above it. So that where the word ends the next space on the line below it is where the next word will start
 
Old 04-21-2004, 04:55 PM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Code:
#!/bin/bash

SPACES="============================================================"
NR=0
ARG=$1
while shift ; do
	echo $ARG
	NR=$((NR+${#ARG}))
	ARG=$1
	test -z $ARG || echo -n ${SPACES:0:$NR}
done
 
Old 04-21-2004, 05:18 PM   #5
vsop
LQ Newbie
 
Registered: Apr 2004
Location: MD
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by Hko
Code:
#!/bin/bash

SPACES="============================================================"
NR=0
ARG=$1
while shift ; do
	echo $ARG
	NR=$((NR+${#ARG}))
	ARG=$1
	test -z $ARG || echo -n ${SPACES:0:$NR}
done
That won't work with regular spaces. It works fine with the '=' char, but if you replace it with a blank spaces it does not work

I got the code from david_ross
to work just now..

Code:
#!/bin/bash

IFS="
"
count=0;
for line in `cat infile`;do
 for ((num=0;$count != $num;num=$(($num+1))));do
  echo -n " "
 done
 echo $line
 num=`echo $line | wc -m`
 num=$(($num-1))
 count=$(($count+$num))
done
I just added the line num=$(($num-1)) to get rid of the spacing issue..

Thanks For the help
 
Old 04-21-2004, 05:47 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
Quote:
Originally posted by vsop
That won't work with regular spaces. It works fine with the '=' char, but if you replace it with a blank spaces it does not work
True.
But to fix this, you only need to put double quotes around ${SPACES:0:$NR}.
Then it does work.
Code:
SPACES="                                                               "
NR=0
ARG=$1
while shift ; do
	echo $ARG
	NR=$((NR+${#ARG}))
	ARG=$1
	test -z $ARG || echo -n "${SPACES:0:$NR}"
done
 
Old 04-21-2004, 06:06 PM   #7
vsop
LQ Newbie
 
Registered: Apr 2004
Location: MD
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by Hko
True.
But to fix this, you only need to put double quotes around ${SPACES:0:$NR}.
Then it does work.
Code:
SPACES="                                                               "
NR=0
ARG=$1
while shift ; do
	echo $ARG
	NR=$((NR+${#ARG}))
	ARG=$1
	test -z $ARG || echo -n "${SPACES:0:$NR}"
done
Nice, did not even think about the double quotes.

Thanks Guys
 
Old 04-22-2004, 12:54 PM   #8
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 58
With my example you should just be able to change the for line:
for ((num=1;$count != $num;num=$(($num+1))));do
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lost pane,issues when creating alias from the bash shell spoult Linux - Software 2 04-19-2005 06:22 AM
Lost a startup script... OoPs Hooper Slackware 11 06-13-2004 09:33 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 05:34 AM


All times are GMT -5. The time now is 03:39 PM.

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
Open Source Consulting | Domain Registration