0

Right. Basically what I want to achieve is a bash script which takes the name of a file as the first argument and displays lines from the input file that are:

  1. Not directories
  2. Executable And these lines should be sorted in an ascending file size order.

The input file example that I'm using has the following content:

-rw-r--r-- 1 root     software       36 Dec  3 14:27 config
-rwxr-xr-x 1 root     software       72 Dec  3 14:27 config2
-rw-rw-r-- 1 jonathan software  7410294 Dec  3 14:28 even larger file
-rwxrwxr-x 1 jonathan software 17290686 Dec  3 14:29 even larger file2
-rwxrwxr-x 1 jonathan software  2470098 Dec  3 14:28 large file
-rwxr-xr-x 1 andrew   software   823366 Feb 11 16:25 myprogram
-rwxrwxr-x 1 jonathan software    29568 Dec  3 14:25 script
drwxrwsr-x 2 andrew   software     4096 Dec  3 19:24 testdir1
drwxrwsr-x 2 bob      software     4096 Dec  3 10:21 testdir2
drwxrwsr-x 2 david    software     4096 Aug 23 14:24 testdir3
drwxrwsr-x 2 david    software     4096 Dec  3 13:32 testdir4
drwxrwsr-x 2 james    software     4096 Jan  5 14:24 testdir5
-rw-rw-r-- 1 james    software       85 Dec  3 14:24 testfile1

I have written the script to ask for the file name and print the content of the file:

#!/bin/bash
echo "Please enter your filename:"
read filename
while read line;
do echo -e "$line\n";
done < $filename

But I don't know what's the command to look for the content line by line and search for lines which don't contain "drw" and also how to sort the lines by the size given in the example input file.

EDIT: This is the 2nd part of my problem. Now I want to modify the script to take a second argument, which if supplied is assumed to be the user name. Only lines matching the user name will be printed.

Eg: "jonathan" would print the following:

-rwxrwxr-x 1 jonathan software    29568 Dec  3 14:25 script
-rwxrwxr-x 1 jonathan software  2470098 Dec  3 14:28 large file
-rwxrwxr-x 1 jonathan software 17290686 Dec  3 14:29 even larger file2
1
  • How are you getting input data like this? from ls's output? Commented Jan 17, 2014 at 16:54

2 Answers 2

2

Here is a solution with awk:

$ awk '!/^d/ && $1 ~ /x/' text.txt | sort -k5,5n
-rwxr-xr-x 1 root     software       72 Dec  3 14:27 config2
-rwxrwxr-x 1 jonathan software    29568 Dec  3 14:25 script
-rwxr-xr-x 1 andrew   software   823366 Feb 11 16:25 myprogram
-rwxrwxr-x 1 jonathan software  2470098 Dec  3 14:28 large file
-rwxrwxr-x 1 jonathan software 17290686 Dec  3 14:29 even larger file2
  • You filter out things that start with a d
  • You check for things whose first field contains an x
  • You sort based on the 5 field, numerically

Here is a version you can drop in a shell script:

file=$1
user=$2

awk '!/^d/ && $1 ~ /x/ && $3 ~/'"${user}/" "$file" | sort -k5,5n
Sign up to request clarification or add additional context in comments.

10 Comments

Not much familiar with awk tool mate. How do I implement that awk command in my bash script?
@BhanuChawla You might get away with just calling that but with $1 instead of text.txt.
I have edited the question and added another issue I need to solve. I guess awk will solve that. Can you help mate? :)
@BhanuChawla Add another && $3 ~ /$2/ maybe ?
Sorry to be a noob but what will be the final script? Can you update your answer? I tried awk '!/^d/ && $1 ~ /x/' text.txt | sort -k5,5n && $3 ~ /$2/ in my script but it's not working. pastebin.com/dERMnRzM
|
0

using shell.

ls -l |grep -v "^d" |grep "^-rwx"|sort -k5n

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.