LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2010, 10:31 AM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Rep: Reputation: 55
pass source file into awk script


Hi All,

I have following awk script test.sh

Code:
#!/usr/bin/awk -f
BEGIN { FS=":" }
{
"groups "$1"| cut -d ':' -f2 | cut -d ' ' -f2" | getline GROUP
print "RESULT_START __NAME__:"$1":PRIMARY_GRP:"GROUP":RESULT_END"
}
I am executing this script with following command
Code:
awk -f test.sh /etc/passwd
But I want this to get executed without any arguments like
Code:
./test.sh
I gave executable permission to test.sh script and tried to add /etc/passwd entry in the script. Still I could not succeed.

Please advice
 
Old 07-14-2010, 10:40 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
why on earth have you called it test.sh when it's not a shell script???

Personally I would make it a proper shell script, and just pipe the passwd file within the script, and not bother with a specific awk script, as it's pretty tiddly.

Code:
#!/bin/sh
awk 'BEGIN { FS=":" } { 
     "groups "$1"| cut -d ':' -f2 | cut -d ' ' -f2" | getline GROUP
     print "RESULT_START __NAME__:"$1":PRIMARY_GRP:"GROUP":RESULT_END"} 
' /etc/passwd

Last edited by acid_kewpie; 07-14-2010 at 10:47 AM.
 
Old 07-14-2010, 10:52 AM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Original Poster
Rep: Reputation: 55
Many thanks acid_kewpie for your response.

Quote:
#!/bin/sh
awk 'BEGIN { FS=":" } {
"groups "$1"| cut -d ':' -f2 | cut -d ' ' -f2" | getline GROUP
print "RESULT_START __NAME__:"$1":PRIMARY_GRP:"GROUP":RESULT_END"}
' /etc/passwd
I have already tried this..This gives following error
Code:
[Rohit@localhost test]$ ./test
awk: cmd. line:1:      "groups "$1"| cut -d : -f2 | cut -d 
awk: cmd. line:1:                 ^ unterminated string

Last edited by vinaytp; 07-14-2010 at 10:55 AM.
 
Old 07-14-2010, 11:02 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Firstly, the following is not required:
Quote:
awk -f test.sh /etc/passwd
You already have the interpreter at the top of the file so you can simply call it with:
Code:
./test.sh /etc/passwd
I agree I would change the extension in this case as it is not a shell script but an awk one ... maybe test.awk

As for the file, if you do not include a file as an argument it will think you want the script applied to standard input, which will obviously not work.
 
Old 07-14-2010, 12:51 PM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by grail View Post
Firstly, the following is not required:

You already have the interpreter at the top of the file so you can simply call it with:
Code:
./test.sh /etc/passwd
I agree I would change the extension in this case as it is not a shell script but an awk one ... maybe test.awk

As for the file, if you do not include a file as an argument it will think you want the script applied to standard input, which will obviously not work.
I agree with you grail, But I want to include /etc/passwd inside the script.

I would like to execute the script as
Code:
./test
without /etc/passwd as arguments.
 
Old 07-14-2010, 02:30 PM   #6
whk
Member
 
Registered: Jun 2005
Posts: 202

Rep: Reputation: 37
This may sound kind of over doing it but just make another vi test and paste

awk -f test.sh /etc/passwd

Now u can type ./test

BTW, careful with test cause it's a Bash default. As long as u keep it in path ./
 
Old 07-14-2010, 03:36 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by vinaytp View Post
Many thanks acid_kewpie for your response.



I have already tried this..This gives following error
Code:
[Rohit@localhost test]$ ./test
awk: cmd. line:1:      "groups "$1"| cut -d : -f2 | cut -d 
awk: cmd. line:1:                 ^ unterminated string
You should fix your code then, nothing to do with my suggestion, maybe my cutting and pasting though...
 
Old 07-14-2010, 05:50 PM   #8
igc35
LQ Newbie
 
Registered: Feb 2005
Location: Tasmania
Distribution: Fedora
Posts: 2

Rep: Reputation: 2
I think you may be victim of an old trap that I fell into many years ago.

You cannot call a script "test" if you want to execute it by name. test.sh or test.awk is ok, but test is a built in shell command, which will always take precedence over execution of a script.
 
Old 07-14-2010, 06:00 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by igc35 View Post
I think you may be victim of an old trap that I fell into many years ago.

You cannot call a script "test" if you want to execute it by name. test.sh or test.awk is ok, but test is a built in shell command, which will always take precedence over execution of a script.

Ummm ... no.


Code:
./test
is clearly distinct from the bash builtin.


Cheers,
Tink
 
Old 07-14-2010, 09:14 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Quote:
Originally Posted by vinaytp
I would like to execute the script as
Code:
./test
without /etc/passwd as arguments.
I think you need to re-read my post ... I wasn't saying I couldn't do it, but rather that as far as I know what you want is not possible, from the single script (obviously whk's suggestion will work)

Try reading this page. It is part of the link I gave you previously
 
Old 07-14-2010, 10:23 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Simplified (less elegant, but working) version:
Code:
awk -F: '{print $1}' /etc/passwd|xargs groups |awk '{print "User: "$1"\t\t primary group: " $3}'
 
Old 07-15-2010, 03:38 AM   #12
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 707

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by Tinkster View Post
Simplified (less elegant, but working) version:
Code:
awk -F: '{print $1}' /etc/passwd|xargs groups |awk '{print "User: "$1"\t\t primary group: " $3}'
Yes, This works well..

Thanks everybody for responses.
 
  


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
how to pass MySQL user/pass securely in shell script? digity Linux - Newbie 5 01-07-2010 05:48 AM
Awk script to select range from file jeesun Linux - General 8 11-26-2009 04:46 AM
Help needed in writing awk script for xml source naren_0101bits Programming 7 10-02-2007 08:47 PM
Help with a script to edit text file (awk? sed?) rickh Linux - Newbie 8 04-21-2005 08:24 PM
pass root password in cgi script file explorer Linux - General 3 04-06-2004 10:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:26 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