LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-10-2014, 02:43 PM   #1
zcarlile
LQ Newbie
 
Registered: Dec 2013
Location: Longview tx
Posts: 5

Rep: Reputation: Disabled
bash variable referencing question.


Hello,

I am new into the bash programming. I am working on a script to use to verify uniformity on large files of sql statements. below is my current code.

Code:
#!/bin/bash

echo "What SQL do ya want to check???"
read fname

##The script is suppose to check your inpute fname to make sure its a regular file. if it is then its suppose to break down the individual parts and echo it out.

if [ -f $fname ]
then
        for i in {1..26}; 
        do echo "-------------------- checking field $i";
        cat $fname | awk -v v="$fname" '{print $v}' | sort | uniq | head; done
 else 
        echo "this is not a file"
 
fi
My question is this: fname refers to the file name input, How would I write the loop? I need to set a varuable for the command but in the loop its going to be refering to fname.
 
Old 01-10-2014, 11:07 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Firstly, quotes, quotes and more quotes. Variables without quotes in bash can cause unpredictable results.

You may need to explain further what you need as the cat, awk, sort, uniq and head output is not clear (I would add that awk can probably do all of those for you)

As for reading a file, use a while, read loop:
Code:
while read var
do
   <your stuff here>
done<"$fname"
Here 'var' will store each line read from the file for you to work on.

If this is not what you require, please explain further?
 
Old 01-14-2014, 09:59 AM   #3
zcarlile
LQ Newbie
 
Registered: Dec 2013
Location: Longview tx
Posts: 5

Original Poster
Rep: Reputation: Disabled
Basically at my job I often deal with 1k+ lines of duplicate SQL statements. My objective is to have the awk break it down so that I can check to make sure that my formating is correct. The following command line will do this.
Quote:
for i in {1..26}; do echo "-------------------- checking field $i"; cat /blah/blah/blah | awk -v v="$i" '{print $v}' | sort | uniq | head; done
will do what I need but I dont want to type the command line every time I need to run this.

My question is this.
I want my syntax as follows:
./script_name file_to_be_checked_name
displays the output from the above command line.

My problem is I have no formal training with bash, and dable in very little.
I know that the loop for in `blah` is naming blah as variable i which is fine, but how to write the code to do variable i on variable fname
 
Old 01-14-2014, 11:12 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Do not use for to read a file. (http://mywiki.wooledge.org/DontReadLinesWithFor)

Simply alter your code to test for an argument (the filename) and then assign using its position on the line ($1 being the first argument) to fname.

I would also maintain that the conglomeration of commands is not required but we would need to see the format of the file and what you are trying to achieve to help further.

Last edited by grail; 01-14-2014 at 11:17 AM.
 
1 members found this post helpful.
  


Reply



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
bash scripting - arrays and indirect referencing soulcurry Programming 7 04-21-2011 06:41 AM
Bash Variable Naming Question? Speedy2k Linux - Newbie 7 06-01-2010 08:23 AM
question regarding bash script variable mrmnemo Linux - Desktop 4 03-22-2010 12:55 PM
BASH variable referencing SwingingSimian Programming 3 10-16-2008 05:20 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:18 AM.

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