LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-28-2010, 01:47 PM   #1
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Rep: Reputation: 0
Using variable to store command lines in a pipeline?


Hi,

Was trying to write a shell script that has if conditional statements to decide different arguments for a command.

Basically:

Code:
if [ "$1" = 1 ]
then
{
        sortarg=$("sort")
}
elif [ "$1" = 2 ]
then
{
	sortarg=$(echo "sort -k2 -t :")
}
fi

cat Cars.txt | "$sortarg"
However, I keep getting the "sort -t : Command not found" error for the 2nd option. My sort command has been verified to work if I hard code the entire sort command line. ie.

Code:
cat Cars.txt | sort -k2 -t :
The sort is working fine for the first option.

I have tried many different ways of phrasing the whole sort line in $sortarg but to avail. The reason I want to do this is due to the fact that the cat line is part of a very big loop, and different sorting options are available to the user. If I do not store the whole sort command line in a variable, I will have to copy-paste the whole chunk of code a few times for each sorting option.

Please assist.

TIA.!

Last edited by zizou86; 01-28-2010 at 02:00 PM.
 
Old 01-28-2010, 02:06 PM   #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,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Code:
if [ "$1" = 2 ]
then
	sortarg="-k2 -t :"
fi

cat Cars.txt | sort $sortarg
BTW, in bash, [[ <test expression> ]] is better than [ <test expression> ] for reasons explained here.

EDIT: the reason it was failing in the second case is that the double quotes around "$sortarg" makes it a single "word" so the shell was looking for command called "sort -k2 -t :" rather than the sort command with options. You could fix the script by removing the double quotes but the above is simpler.

Last edited by catkin; 01-28-2010 at 02:09 PM. Reason: Lost colon
 
Old 01-28-2010, 02:10 PM   #3
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
take the " off of "$sortarg".
 
Old 01-28-2010, 02:48 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
The BashFAQ has an entry on this exact issue:
http://mywiki.wooledge.org/BashFAQ/050
 
Old 01-29-2010, 07:54 AM   #5
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Hi all,

Thanks for the great help thus far. The original program worked great! I tried to add some more stuff to this.

Code:
if [ "$1" = 1 ]
then
{
        sortarg=$("sort")
}
elif [ "$1" = 2 ]
then
{
	sortarg=$(echo "sort -k2 -t :")
}
fi

while read inputline
	do
		col1="$(echo $inputline | cut -d: -f1)"
		col2="$(echo $inputline | cut -d: -f2)"
		col3="$(echo $inputline | cut -d: -f3)" 
		col4="$(echo $inputline | cut -d: -f4)"	
		col5="$(echo $inputline | cut -d: -f5)"	
		
	done < Cars.txt | $sorter
However, this does not seem to work. I had already verified that my sort commands worked via "cat Cars.txt | sort sort -k2 -t : " in the shell prompt. The reason I am adding this in is to split the data into different columns.

However, once inside the program it does not seem to work. Only good old sort without any arguments worked.

Am I missing something here?

Thanks.
 
Old 01-29-2010, 08:14 AM   #6
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,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
$sorter is not given a value in the code snippet you posted.

A useful debugging technique is to add lines showing the value of variables, for example
Code:
while read inputline
	do
		echo "DEBUG: inputline: '$inputline'"
		col1="$(echo $inputline | cut -d: -f1)"
		echo "DEBUG: col1: '$col1'"
                [snip]
The purpose of the single quotes is to show any leading or trailing whitespace in the value of the variable.

BTW, you do not need to echo strings to assign them to variable, you can assign them directly, for example
Code:
sortarg='sort'
You are, of course, free to ignore any suggestions given here but have you understood how much simpler the code I suggested above is than the code you are using?
 
Old 01-29-2010, 11:20 AM   #7
zizou86
LQ Newbie
 
Registered: Jan 2010
Posts: 16

Original Poster
Rep: Reputation: 0
Code:
	if [ "$1" = 1 ]
	then
	{
		sorter=""
	}
	elif [ "$1" = 2 ]
	then
	{
		sorter="-k2 -t :"
	}
	fi

	cars=$(cat Cars.txt | sort $sorter)
	echo "$cars" > Cars.txt

	while read inputline
	do
		col1="$(echo $inputline | cut -d: -f1)"
		col2="$(echo $inputline | cut -d: -f2)"
		col3="$(echo $inputline | cut -d: -f2)"
		col4="$(echo $inputline | cut -d: -f2)"
		col5="$(echo $inputline | cut -d: -f2)"

		printf "%-45s %-20s %-10s %-5s %-8s %-10s\n" "$col1" "$col2" "$col3" "$col4" "$col5" "$total_sales"
	done < Cars2.txt # display in sorted format!
        ##done < Cars.txt | sort $sorter  ###THIS WAS ORIGINAL COMMAND THAT DO NOT WORK!
Output:

Car 1 Mitsubihi 13 22 50

Hence, $col1 to 5 will hold the value of the respective field in each row, looping till the whole file is complete.

Code:
Data of Cars.txt (quite a few rows, delimited by colons):

format: Car model:Manufacturer:price:no. sold:no. left

Car 1:Mistubishi:13:22:50
Car 5:Toyota:23:3:2


Catkin,

My apologies for ignoring your earlier post about leaving the sort command out of the string. I did not really notice the difference at first and hence did not carry out your suggestion.

Basically, what this do was to grab the data off Cars.txt and display out all rows in a format. What I wanted to do was to add a "sorting function" so you can sort by Car name, brand, price, etc. So far, I have only added 2 of these.

My original intention was to dynamically assign $sorter value. Hence, the user will select what to sort with, and $sorter will hold the relevant sort command's arguments for that particular selection. However, apparently there is some form of logic error in this as this does not work in the loop. I have verified that this method does work provided if I simply used:

Code:
cat BookDB.txt | sort $sorter
Hence, the interim solution I employed is to sort the file first, store it inside another file (the read line do while loop refuse to let me throw in a variable, only wants a file) and let the sorted file display out the contents. Not very elegant, but at least its working somewhat.

However, I would appreciate if any of you experienced users will care to break down and explain why my intial method do not work? (The intial method is in the code with a ###THIS WAS ORIGINAL COMMAND THAT DO NOT WORK! comment.

Thank you.
 
  


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
how to pipeline when the file requires two command line args???? lemon09 Programming 4 01-19-2010 10:24 PM
How to store SQL output in a variable atulsh General 1 09-18-2008 02:22 PM
a pipeline for counting the lines in a directory fara77 Linux - Newbie 1 03-05-2007 07:00 PM
Store PATH variable permanently Skute Linux - Software 8 01-15-2004 05:05 PM
Iptables, where do i store NAT lines? d33pdream Linux - Software 1 03-18-2003 01:20 AM


All times are GMT -5. The time now is 11:42 PM.

Main Menu
Advertisement
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