Sponsored Content
Homework and Emergencies Homework & Coursework Questions Help with exit, grep, temporary files, awk Post 302680317 by BartleDoo on Wednesday 1st of August 2012 04:26:09 PM
Old 08-01-2012
Hammer & Screwdriver Help with exit, grep, temporary files, awk

1. The problem statement, all variables and given/known data:
I do not understand how/why the following code is used. Please do not simply refer me to the man pages since I have already reviewed them extensively. Thank you.

exit 2 , exit 3, exit 0
I understand the basics of why the exit command is used, but I still don't understand what the significance/meaning of exit 2, exit 3, and exit 0 are.
The creation (and later deletion) of a temporary file
I understand that creation of the temporary file is used to print the contents of the following commands within that file so the grep command can be used to calculate our echoed results that we want, BUT, isn't there a better way to do it so we don't have to create/delete a file?
ls -log "$1" | awk '{print $1}' | grep -v total > $TF
I understand how the first part is piped to the awk, but I do not understand how the awk command is printing only the first column of information.
As far as the "grep -v total" goes, am I correct in that that command basically says to print to screen only those lines within that first column that do NOT contain the word "total"?
f_count=$(grep -c "-........." $TF
I do not understand why this will not work, however, it also doesn't seem like the best way to accomplish counting the number of files within the specified directory.

2. Relevant commands, code, scripts, algorithms:
Code:
#!/bin/sh

# Check correct number of arguments (one)
# "$#" Stores the # of command-line arguments passed to shell program
if [ $# -ne 1 ]
then
  echo "Usage: homework4.sh <directory_name>"
  exit 2
# Check for directory existence
else
  if [ ! -d "$1" ]
  then
    echo "$1: No such directory"
    exit 3
  fi
fi

# Set up a temporary file name
TF=/tmp/$0.tmp

# Prints only the permissions of all files and directories to $TF
ls -log "$1" | awk '{print $1}' | grep -v total > $TF
# Count (-c) the number of lines where the character ("d", "r", etc.) exists
d_count=$(grep -c "d" $TF)
f_count=$(grep -c "-........." $TF)
r_count=$(grep -c "r" $TF)
w_count=$(grep -c "w" $TF)
x_count=$(grep -c "x" $TF)
# Print the results to screen
echo -n "In the directory "; pwd
echo "  Number of directories     : $d_count"
echo "  Number of files           : $f_count"
echo "  Number of readable items  : $r_count"
echo "  Number of writable items  : $w_count"
echo "  Number of executable items: $x_count"

# Remove temporary file
rm $TF
exit


3. The attempts at a solution (include all code and scripts):
The above code has been modified extensively by me and shows the final attempts of my efforts.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
The Florida State University, Tallahassee Florida, USA, William Street, COP3353 (ww2.cs.fsu.edu/~street/cop3353/hw/hw04.html)
 

7 More Discussions You Might Find Interesting

1. Ubuntu

Avoid creating temporary files on editing a file in Ubuntu

Hi, My ubuntu flavor always create temporary files having filename followed by ~ on editing. For eg: if I am editing a file called "sip.c", automatically a temporary (bkup) file is getting created with the name "sip.c~". How to avoid this file creation? (7 Replies)
Discussion started by: royalibrahim
7 Replies

2. Shell Programming and Scripting

Temporary files and rm

Hello everyone, I am creating a temporary file in my ksh by using something file filetemp=filetemp.$$ Later on in my script I write to the file $filetemp by 'cat'ing to it. Then in the script I am doing a 'less' on the file to view it. At the end of the script I issue a rm $filetemp 2>... (4 Replies)
Discussion started by: gio001
4 Replies

3. Shell Programming and Scripting

Writing files without temporary files

Hey Guys, I was wondering if someone would give me a hand with an issue I'm having, let me explain the situation: I have a file that is constantly being written to and read from with updated lines: # cat activity.file activity1 activity2 activity3 activity4 activity5 This file... (2 Replies)
Discussion started by: bashshadow1979
2 Replies

4. Shell Programming and Scripting

Help - Bug: A script to compile two types of data files into two temporary files

Dear other forum members, I'm writing a script for my homework, but I'm scratching all over my head and still can't figure out what I did wrong. Please help me. I just started to learn about bash scripting, and I appreciate if anyone of you can point out my errors. I thank you in advance. ... (3 Replies)
Discussion started by: ilove2smoke
3 Replies

5. Shell Programming and Scripting

Rsync temporary files

Hi, I am looking to use rsync in a very specific way, and even though I have trawled the rsync man pages I have not succeeded in seeing a way of doing the following: The temporary files created by rsync should not be created in the destination directory. (I have used --temp-dir option to... (0 Replies)
Discussion started by: LostInTheWoods
0 Replies

6. UNIX for Advanced & Expert Users

Which time should be used for deleting temporary files?

I want to create a folder for users to put their temporary files and a folder for users to put their permanent files. For the temporary folder, I need to implement a deletion policy. I would like to know normally which time, ctime, mtime or atime, should be used to implement such deletion policy. (1 Reply)
Discussion started by: marctc
1 Replies

7. AIX

Hidden temporary files in AIX

Hi, Some porocess is creating hidden temporary files in /tmp directory. And they are not getting deleted. System is going out of disk space after some days. The temp files are getting created like .<user name><pid>. I have checked the application code, but didnt get any clue. Does these files... (4 Replies)
Discussion started by: viswath.sen
4 Replies
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for lines containing a given pattern SYNOPSIS
grep [-elnsv] pattern [file] ... OPTIONS
-e -e pattern is the same as pattern -c Print a count of lines matched -i Ignore case -l Print file names, no lines -n Print line numbers -s Status only, no printed output -v Select lines that do not match EXAMPLES
grep mouse file # Find lines in file containing mouse grep [0-9] file # Print lines containing a digit DESCRIPTION
Grep searches one or more files (by default, stdin) and selects out all the lines that match the pattern. All the regular expressions accepted by ed and mined are allowed. In addition, + can be used instead of * to mean 1 or more occurrences, ? can be used to mean 0 or 1 occurrences, and | can be used between two regular expressions to mean either one of them. Parentheses can be used for grouping. If a match is found, exit status 0 is returned. If no match is found, exit status 1 is returned. If an error is detected, exit status 2 is returned. SEE ALSO
cgrep(1), fgrep(1), sed(1), awk(9). GREP(1)
All times are GMT -4. The time now is 03:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy