Help with simple script with counter

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Help with simple script with counter
# 8  
Old 10-21-2014
So ((count++)) is not suggested?
Or is that as well just bash,ksh93 and zsh?
# 9  
Old 10-21-2014
Unfortunately yes.
This User Gave Thanks to MadeInGermany For This Post:
# 10  
Old 10-22-2014
Quote:
Originally Posted by MadeInGermany
is Bourne shell, works in any shell.
it is Bourne shell, yes, and will work in any shell compatible with the Bourne shell, yes, but will not work in shells not compatible with the Bourne shell.

Generally, backwards compatibility with the Bourne shell is of course a valuable asset nowadays. But wouldn't it be better to go the whole distance? The Bourne-shell was built as an extension to the Thompson-shell (osh) in which the backticks won't work either. To avoid backwards compatibility problems with systems lacking a Bourne-shell and having only a Thompson-shell (like, for instance, most pre-SysV-systems as well as perhaps all the Multics systems) maybe a completely different approach would be best?

Quote:
Originally Posted by MadeInGermany
While
Quote:
(( count += 1 ))
only works in ksh93 and bash and zsh
Actually it works in any ksh, not only ksh93. In fact this is exactly what i said:

Quote:
Does the same in ksh and bash
I still have to see a "POSIX shell" which is not either a "POSIX-compatible ksh" or a "POSIX-compatible bash", therefore i think that the POSIX-issue is moot in this case.

I hope this helps.

bakunin

Last edited by bakunin; 10-22-2014 at 09:10 PM..
# 11  
Old 10-22-2014
A pregnant lady goes to the clinic all concerned. In the process of addressing her concerns, a doctor discovers something abnormal in the unborn.

Another doctor knocks the door opening it ajar.

"Come on in, Doctor!" Let me show you the umbilical hernia that this baby has.
"Sorry, Doctor!" This looks to me more like a gastroschisis.

"Knock, knock!" - Another doctor interrupts at the now open door.

"Doctor! Come in! Take a look at this umbilical hernia"
"That's an exomphalos!" - Authoritatively, this latest doctor declares - "Poor 'bugger' is going to require surgery"


Is the thread starting to sound as such? - I ask myself.
Yes! Too many doctors.

No offense guys. Just an observation.
# 12  
Old 10-22-2014
Bakunin, you are right, (( count+=1 )) works in older ksh, too. (But not (( count++ )).)
Maybe I tested with another Posix shell: posh, dash, ...
# 13  
Old 10-25-2014
Thanks for the input. I am not sure the other suggested shell scripts work in my bash shell, and the professor wants it a specific way. My script now looks like this.
Code:
     1  count=1
     2  if [ -d "$1" ]
     3    then
     4    echo "The diretory $1 exists."
     5  else
     6    echo "The directory $1 does not exist."
     7  fi
     8   if [ -e "$1" ]
     9    then
    10  echo "The file $1 exists and has the following properties: "
    11  fi
    12  echo "Please enter another filename or an x to exit the script: "
    13  while [ "$filename" != "x" ]
    14  do
    15  read filename
    16   if [ -r "$1" ]
    17    then
    18    echo -n "Read. "
    19   fi
    20   if [ -w "$1" ]
    21   then
    22   echo -n "Write. "
    23  fi
    24   if [ -x "$1" ]
    25   then
    26   echo -n "Execute."
    27  fi
    28  echo " "
    29  if [ -k "$1" ]
    30   then
    31   echo "Sticky bit is set."
    32   else
    33   echo "Sticky bit is not set."
    34  fi
    35  if [ -s "$1" ]
    36   then
    37   echo "The file is not empty."
    38   else
    39   echo "The file is empty."
    40  fi
    41  COUNTER=`expr $COUNTER + 1`
    42  echo "Please enter a filename or an x to exit the script: "
    43  read filename
    44  done
    45  echo "$COUNTER"

The problem is no matter what file I test it against it doesn't seem to be reading "$1". It only outputs
Code:
Read. Write.
Sticky bit is not set.
The file is not empty.
Please enter another filename or an x to exit the script:

. Even when I press x to exit, it will output the above and then exit. The counter still doesn't seem to be working properly. Thanks in advance
# 14  
Old 10-25-2014
$1 is the parameter with what you run the script e.g.
Code:
./myscript thisfile

and $1 becomes thisfile.
Your read goes to variable filename. You get its value by $filename
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