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 ] Code:
cat Cars.txt | sort -k2 -t : 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.! |
Code:
if [ "$1" = 2 ] 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. |
take the " off of "$sortarg".
|
The BashFAQ has an entry on this exact issue:
http://mywiki.wooledge.org/BashFAQ/050 |
Hi all,
Thanks for the great help thus far. The original program worked great! I tried to add some more stuff to this. Code:
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. |
$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 BTW, you do not need to echo strings to assign them to variable, you can assign them directly, for example Code:
sortarg='sort' |
Code:
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): 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 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. |
All times are GMT -5. The time now is 01:19 PM. |