Help with simple script with counter

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with simple script with counter
# 1  
Old 10-20-2014
Help with simple script with counter

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

I am to create a script that checks a file for specific attributes. Read, write, execute, if the file is empty, and if the sticky bit is set. I have completed this step but I am stumped on how to add a simple counter and to have the script repeat the process of identifying access rights as many times as the person running the script wants. I am to prompt the user to enter another file name or an X to exit the script.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
Code:
     1
     2  count = 1
     3  echo -n "Please enter a filename: "
     4  read filename
     5  while ["$1" != "$filename"]
     6  do
     7   echo "Invalid filename..."
     8   read filename
     9  done
    10  if [ -d "$1" ]
    11    then
    12    echo "The diretory $1 exists."
    13  else
    14    echo "The directory $1 does not exist."
    15  fi
    16   if [ -e "$1" ]
    17    then
    18  echo "The file $1 exists and has the following properties: "
    19  fi
    20   if [ -r "$1" ]
    21    then
    22    echo -n "Read. "
    23   fi
    24   if [ -w "$1" ]
    25   then
    26   echo -n "Write. "
    27  fi
    28   if [ -x "$1" ]
    29   then
    30   echo "Execute."
    31  fi
    32  if [ -k "$1" ]
    33   then
    34   echo "Sticky bit is set."
    35   else
    36   echo "Sticky bit is not set."
    37  fi
    38  if [ -s "$1" ]
    39   then
    40   echo "The file is not empty."
    41   else
    42   echo "The file is empty."
    43
    44  count=`expr $count+1`
    45  fi
    46  echo "Your total count is"
    47  echo "$count"

any help would be greatly appreciated as I am completely stumped. If i remove the count syntax's, it prompts me to enter a filename but displays an error for line 5.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)
Westchester Community College, NY, United States, Cordino, COMSC118
# 2  
Old 10-21-2014
There should be spaces after `[' and before `]' like so:
Code:
while [ "$1" != "$filename" ]

# 3  
Old 10-21-2014
As opposed to variable assignments where there should be no spaces around the assignment operator (hint). Also have a look at what the meaning of $1 is. It is often used to pass parameters (command line values) to a script, so the while loop does not seem to make much sense here and also the read statement needs to put its outcome somewhere in a variable, plus the count variable is not incremented inside a loop.

Last edited by Scrutinizer; 10-21-2014 at 11:26 PM..
# 4  
Old 10-21-2014
It is not unusual to forget about good coding practices when writing scripts. This is wrong. You already have a script which checks for one file one time. Why don't you put what you have into a function (read about shell functions in the man page of your shell) and call that function?

If you have a command, say: "foocommand", and want to execute it a 100 times, this usually looks like (pseudocode)

Code:
while counter lower than 100
     foocommand
     counter ++
end while

Wouldn't it be easier to write something like this in shell code, instead of cramming all the functionality into one big loop? Incidentally it would also bring a solution to your other problem once you figure out how to pass parameters to a function.

Anopther thing:

Code:
count=`expr $count+1`

This is neither wrong nor illegal, but you should avoid that anyway. Backticks are outdated and deprecated and generally not considered good style. Furthermore, in modern shells you do not need "expr" because any shell has a built-in way to handle integer operations. This:
Code:
(( count += 1 ))

Does the same in ksh and bash and is easier to read too. If you need process substitution (in this case you don't) you wouldn't use backticks either but:

Code:
var=$(/some/command | /other/command | .... )

I hope this helps.

bakunin

Last edited by bakunin; 10-21-2014 at 01:52 PM..
# 5  
Old 10-21-2014
Quote:
count=`expr $count+1`
is Bourne shell, works in any shell.
If there is a Posix/standard shell (or ksh or bash or zsh) the following is recommended because it avoids the external program expr
Code:
count=$(( count+1 ))

While
Quote:
(( count += 1 ))
only works in ksh93 and bash and zsh, and has no further speed enhancement.
This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 10-21-2014
Staying POSIX, one can even use:

Code:
i=0
while [ $((i+=1)) -le 100 ]

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 10-21-2014
Hmm, interesting. Consequently the following is Posix, too:
Code:
: $(( count+=1 ))

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Automatic counter script

Hello, I am having trouble calculating some numbers and I was hoping someone could help me solve this. I have one file with 1 column and what I'm trying to do is add up the lines until a certain value is reach, then jump to where it last finished counting and continue. so for ex: if I... (27 Replies)
Discussion started by: verse123
27 Replies

2. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND="ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/deployall.sh >/dev/null 2>&1 &" ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

How to display a counter in shell script?

Hi, I am writing a script which processes large number of files in a directory. I wanto display a counter which increment after processing each file. I am processing each file in a for loop. If I echo a variable with its value increasing with each file, I will get around 5000 lines as output.... (10 Replies)
Discussion started by: jaiseaugustine
10 Replies

5. Shell Programming and Scripting

Reset the counter in shell script

I am executing the following script using 'awk -f process.awk out' where 'out' is the input file which consists of 5000 sequences. Each time it takes one sequence, run the below program by creating a directory, run the mfold command within that directory, running another shell script 'final5' and... (2 Replies)
Discussion started by: kswapnadevi
2 Replies

6. Shell Programming and Scripting

cant get a counter to work in bash scipt, this is calling expect script

I have looked high and low, tryed lots of diffrent things but cant get a simple counter to work right. what i need is to increase a count ever time it finishes the test, pass or fail. example TEST PASS 1, NEXT TEST PASS 2, I curently have set foo o while {$foo <=5} { incr foo puts... (1 Reply)
Discussion started by: melvin
1 Replies

7. Shell Programming and Scripting

Accumulate counter in script

Hi, I'm new to unix and have a problem? I'm writing a basic script in ksh and it is a basic quiz with 5 questions. I need to be able to accumulate the correct answers at the end and echo out the total correct answers, I cannot work it out? Please see script so far. If anyone can help that will... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

8. Shell Programming and Scripting

Counter Script..help please

I have generated a script that will email a list of people if a certain PID is not running, using "mailx". I have the script running every 5 minutes as a cron job. I want the script to stop sending an email, if the email has been sent 5 times (meaning PID is dead). I want this so that my... (3 Replies)
Discussion started by: Sunguy222
3 Replies

9. UNIX for Dummies Questions & Answers

Error in Script (counter)

Hello: Executing following script i'm getting error: 1=1+1: 0403-058 Assignment requires an lvalue. It's not assuming the counter but i don't know why. Some hint? Thank you very much in advance. #!/bin/ksh <path>/tiempos.txt num_exe=1 TIEMPOS=<path>/tiempos.txt while ] do (2 Replies)
Discussion started by: Felix2511
2 Replies
Login or Register to Ask a Question