Register
It is currently Fri Apr 18, 2014 2:03 am

using awk inside of a variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Mon Jun 13, 2011 11:11 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Hello.

I am writing a script that queries the permissions on certain files and displays them in a neat form.

When I run this command string:
Code:
ls -la /etc/hosts | awk '{print $1 " " $2 " " $3 " " $4}'

I get this (which is all that I want displayed in the results from the script):
Code:
-rw------ 1 root root

(btw, I added the " " to the awk argument for a nicer display)

Now, I have several files I want to do this to, so why repeat the awk argument over and over. So, I thought about putting it into a variable:
Code:
PERMS=awk '{print $1 " " $2 " " $3 " " $4}'
ls -la /etc/hosts | $PERMS

but the script never returns anything. I have tried various things, such as adding echo to the variable, but to no avail. Does anyone know of the magic 'thing' that I need to do to get this work?

Thanks...and, sorry, but my awk knowledge is not that great.


Top
 Profile  
 PostPosted: Mon Jun 13, 2011 12:10 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
You could create a function instead and pipe the output through that.
Code:
function perms { awk '{print $1,$2,$3,$4}'; }
ls -la /etc/hosts | perms


Top
 Profile  
 PostPosted: Mon Jun 13, 2011 12:30 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Patsie wrote:
You could create a function instead and pipe the output through that.
Code:
function perms { awk '{print $1,$2,$3,$4}'; }
ls -la /etc/hosts | perms


you know, I tried that earlier, but left off the ; at the end of the function ...so thanks for pointing that out and the help. It works great!


Top
 Profile  
 PostPosted: Mon Jun 13, 2011 1:54 pm   
User avatar

Joined: Wed Nov 17, 2010 1:02 pm
Posts: 9
Location: North Bethesda, MD
Try it like this:

Code:
PERMS="$(ls -la /etc/hosts | awk '{print $1 " " $2 " " $3 " " $4}')"
echo $PERMS


The output is the value of variable PERMS, and you can do whatever you want with it.


Top
 Profile WWW  
 PostPosted: Mon Jun 13, 2011 2:19 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Don't parse ls(1) output.
You can use stat(1).

Code:
#!/bin/bash

getperms() {
    # usage: getperms <(file|directory) name>
   
    if (($# != 1)) || [[ ! -e $1 ]]; then
        printf 'E: one filename or directory name has to be passed to %s().\n' "$FUNCNAME" >&2
        return 1
    fi

    stat --printf '%A %h %U %G\n' "$1"
}

# example using getperms()
getperms /etc/hosts


Also, to assign a command / function output to a variable you have to use $(...).
For example: myvar=$(getperms /etc/hosts) (notice that myvar is not written with uppercase letters, because if it does it'd mean it's a shell environment variable (by convention))


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP