LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-07-2007, 05:53 AM   #1
kalyanofb
LQ Newbie
 
Registered: Feb 2007
Posts: 21

Rep: Reputation: 15
awk - check $1 is a linux command or not?


hai friends,

i am reading a file in awk and check whether $1 is linux command or not.

I don't know how to check the $1 is linux command or not.

Please help me
 
Old 02-07-2007, 07:09 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
In an awk program, $# refers to a field in the line read. $0 refers to the entire line.

Check if you can install a "gawk-doc" package if available. I contains an excellent book: "Gawk: Effective AWK Programming"
 
Old 02-07-2007, 07:27 AM   #3
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
umm.. if you're $PATH is setup propperly, you could do it quite easily in shell-scirpt....

Code:
#!/usr/bin/ksh
INPUT=$1

cat ${INPUT} | while read app
do
  which ${app} >/dev/null 2>&1
  [[ $? -eq 0 ]] && {
    echo "${app} is a linux command!"
    continue
  }
  echo "${app} is not a linux command!"
done
So, if you saved the above code in a file called "check.sh" and you had a file with commands (one per line) called commands.txt you would run:

/path/to/check.sh /path/to/commands.txt
 
Old 02-07-2007, 07:37 AM   #4
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
If you must do this in awk, then the system() call might help. It would be easiest to invoke something like the scripts already shown, but if you have to make the call in-line, then something like this should work:

system("IFS=:; for dir in $PATH; do if test -x $dir/"$1"; then exit 0; fi; done; exit 1")

or just

system("which "$1)

That returns 0 if $1 is on $PATH and executable, or 1 otherwise. Only tested on HP-UX, but it should work anywhere :-)

Last edited by Nick_Battle; 02-07-2007 at 07:39 AM.
 
Old 02-07-2007, 07:51 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is an awk solution:
Code:
{ 
( "which --skip-alias " $1 " 2> /dev/null" ) | getline command ;
  if ( command == "" )
     print $1,"is not a linux command"
  else
     print $1,"is",command
  command = ""
}
 
  


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
awk command to take average? johnpaulodonnell Linux - Newbie 3 02-07-2007 04:11 AM
Awk - get a parameter from the command line benjalien Programming 1 01-24-2006 09:06 AM
Awk command-line arguments lowpro2k3 Programming 1 03-28-2005 09:09 PM
Sed/Awk command help needed. farmerjoe Programming 3 03-02-2005 11:13 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM

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

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