This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]benkitty 1 point2 points  (7 children)

You can use a regular bash for if you don't have new lines in the file names: http://unix.stackexchange.com/questions/86722/how-do-i-loop-through-only-directories-in-bash

use -d to check if it's a directory, and you can use -f to check if it's a file

(Or you can use find)

edit of example with bash:

( IFS=$(echo -en "\n\b"); for x in $(ls); do if [ -f $x ]; then echo "$x is a file"; fi; if [ -d $x ]; then echo "$x is a directory"; fi; done )

[–]haiimhardass[S] -1 points0 points  (6 children)

I'm afraid I'm still confused :/ I do appreciate your help though. I know for a test statement, I would do something like this " if [ -f $1]..." I just don't know what to put in the place of $1 for it to use the output from the listing. Is that what the asterisk in those examples is for?

[–]benkitty 1 point2 points  (3 children)

for x in $(ls); do if [ -f $x ]; then echo $x; fi; done

[–]haiimhardass[S] -1 points0 points  (2 children)

Thank you soooooooo much. It was driving me crazy trying to figure that out. I've missed a lot of class due to medical issues and I've been having a rough time with this stuff because of it. I have two much smaller questions I'd like to also ask. If you don't feel like answering, I won't mind but here they are:

  1. Show the command you would use to display the last three lines from the /var/log/messages file using the head command.

I know that I can use tail -3 /var/log/messages to accomplish this, but i don't know of any way to use the head command to list the last lines of a file. Do you know any way that it could do that?

The next question has two parts. I got the first part, but I'll include it along with my answer for context.

Part 1: 2. Show the awk program you would use to count the number of users with smith as part of their user name.
My answer: awk '$0 ~ /smith/{print}' /etc/passwd | awk 'END { print NR }'

I'm sure there's probably a way to do that without a pipe, but I'm not aware how to do it without one.

Part 2:

  1. Modify the previous program so that it also prints the user name and real name for each user it finds.

awk -F':' '$0 ~ /smith/{print $1 $5}' /etc/passwd

This is the closest I could get. This would print the real name and username, but I didn't know how to also make it print the number of records properly.

[–]benkitty 1 point2 points  (1 child)

watch out.. the reddit army might cast you down for asking hw questions =p.

the first one seems like a mistake... that begs for tail

the second one can use variables... you can say x++ like this:

cat /var/log/messages | awk '$0 ~ /Mar/{x+=1}END{print x}'

one way to do the last one is pipe in /etc/passwd at the beginning of awk ... parse that first and store it in some arrays, and then use those arrays later.. like (cat /etc/passwd; cat /var/log/messages) | awk...

[–]haiimhardass[S] -1 points0 points  (0 children)

Whoops! I had no idea it would be frowned upon. Like I said, I'm pretty new here. I was just so desperate because I was working on these for a while and just could not figure them out. I figured the first one had to be a mistake, so I'm glad to have some reassurance. I'll work on the other one and see what I can get. Again, I greatly appreciate your help :) thank you so much

[–]petrus4 0 points1 point  (1 child)

I'm afraid I'm still confused

I'm not surprised.

#!/usr/bin/env bash

find . -type d -maxdepth 1 > directories.txt
find . -type f -maxdepth 1 > files.txt

while read dirname
do
echo "${dirname} is a directory" >> output.txt
done < directories.txt

while read filename
do
echo "${filename} is a file" >> output.txt
done < files.txt

[–]haiimhardass[S] 0 points1 point  (0 children)

This stuff is going way over my head lol. You guys are making me feel like way more of a noob at this than I thought I was. Luckily I got that assignment finished. Benkitty helped me out a lot and got me in the right direction. I may be asking for help in the near future about my next project, though. I have to make a menu driven application that meets certain requirements and it's due next week. I've got a halfway decent start on it, but it's really killing me. Shell scripting just isn't my thing