5 Replies - 1077 Views - Last Post: 19 May 2010 - 04:26 AM Rate Topic: -----

#1 L0cky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-May 10

Shell script problem

Posted 18 May 2010 - 08:57 PM

Hey guys, Im running this shell script which outputs student data. But Im having MAJOR problems with printf

It like deletes some of the data, It doesn't output it properly, am I using printf wrong?


This is the function
report()
{
	IFS2=$IFS
	exec 0<"$FILE"

	IFS=":"

	printf "%-10s" STUD# 
	printf "%-20s" Name 
	printf "%-10s" A1  A2  A3  A4  A5  EXE  TUT  EXAM  TOTAL
	echo
	echo "----------------------------------------------------------------------------------------------------------"

	while read STUDNO USRNM A1 A2 A3 A4 A5 e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 TUT EXAM
	do
		IFS=$IFS2
		NAME=`searchPasswd "$USRNM"`
		IFS=":"
		printf "%-10s" "$STUDNO" 
		printf "%-20s" "$NAME"
		printf "%-10s" $A1 $A2 $A3 $A4 $A5 $e1 $TUT $EXAM
		echo
	done
	IFS=$IFS2

	
}


Here is the output

STUD# Name A1 A2 A3 A4 A5 EXE TUT EXAM TOTAL
----------------------------------------------------------------------------------------------------------
5 5 5 2 4 2 5 50
5 5 5 5 5 2 5 100


should be like this

STUD# Name A1 A2 A3 A4 A5 EXE TUT EXAM TOTAL
----------------------------------------------------------------------------------------------------------
<number> <name> 5 5 5 2 4 2 5 60 87

Is This A Good Question/Topic? 0
  • +

Replies To: Shell script problem

#2 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6633
  • View blog
  • Posts: 30,896
  • Joined: 10-May 07

Re: Shell script problem

Posted 18 May 2010 - 09:02 PM

Is this supposed to be C/C++ code?

The syntax for print is :

int main(void) {
  int num=0;
  char str[25]="String";
  printf("%d is a number and %s is a string!\n",num,str);
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 L0cky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-May 10

Re: Shell script problem

Posted 18 May 2010 - 09:09 PM

No it's Shell scrpit in C, similar but not the same
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6633
  • View blog
  • Posts: 30,896
  • Joined: 10-May 07

Re: Shell script problem

Posted 18 May 2010 - 09:12 PM

** Topic moved to Other Languages **
Was This Post Helpful? 0
  • +
  • -

#5 L0cky  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 18-May 10

Re: Shell script problem

Posted 18 May 2010 - 09:39 PM

I thought it might just be printf, but i tried with echo, and If I do the strings then the ints, the ints override the strings >:(
Was This Post Helpful? 0
  • +
  • -

#6 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: Shell script problem

Posted 19 May 2010 - 04:26 AM

To start, you didn't mention if this was for Bash or sh. It does matter.

A few issues, as I see it:

1) You're declaring your IFS incorrectly. It should be IFS=$':' to avoid breakage.
2) read will quit as soon as it reaches an EOL character. This may cause a problem if your input file has varying numbers of fields.
3) Your idea of switching to echo is moot, as all variables in (ba)sh start their life as strings -- so the %s token of printf works fine as a catch-all.

At first glance, this screams out for a program like awk to parse it in about 3-4 lines. Are you not allowed to use it?

Posting a snippet of your input file would be helpful.

This post has been edited by noclaf: 19 May 2010 - 04:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1