LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-31-2019, 11:07 AM   #1
DennyY
Member
 
Registered: May 2018
Location: San Francisco, CA
Distribution: CentOS 7
Posts: 85

Rep: Reputation: 1
Question Another option besides 'expr'


Hi,

Just wondering if there is another option besides using 'expr' for this code?

Thank you for your help,
Denny




Code:
#!/bin/bash

count=0
num=10
while [ $count -lt 10 ]
do
        echo
        echo $num seconds left to stop this process $1
        echo
        sleep 1

num=`expr $num - 1`
count=`expr $count + 1`
done
echo
echo $1 process is stopped!!!
echo
 
Old 08-31-2019, 11:16 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
you could try
Code:
((count++))
or, a for loop
Code:
for ((count=0;count<10;count++));do
     <stuff>
done
edit
since you are counting down

Code:
for ((count=10;count>0;count--));do
     printf "%s\r" "$count seconds left to stop this process $1 "
     sleep 1 # forgot this in first edit
done
printf "\n" # \n is new line, \r was return ( to start of line )
untested, but that should just update the line instead of making new ones each time
I have been lazy with the printf, really you should split it up.

Last edited by Firerat; 08-31-2019 at 11:26 AM.
 
1 members found this post helpful.
Old 08-31-2019, 11:29 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,730

Rep: Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919Reputation: 5919
or

Code:
for ((i = 10 ; i > 0 ; i--)); do
  echo "Seconds left: $i"
  sleep 1
done
I missed your updated post...

Last edited by michaelk; 08-31-2019 at 11:37 AM.
 
1 members found this post helpful.
Old 08-31-2019, 11:39 AM   #4
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by DennyY View Post
Hi,

Just wondering if there is another option besides using 'expr' for this code?
Others have already noted the use of the bash "(( expression ))" syntax for math expressions.

Another alternative you might consider is losing the backticks:
Code:
$( command )
instead of
Code:
`command`
Backticks are darned hard to see and the newer syntax is much easier to spot and allows smarter editors (vim, emacs, etc.) that highlight opening/closing brackets, braces, and parentheses to indicate those expressions---especially when you've forgotten to close an expression in one of those characters. I've never been able to figure out a way of nesting expressions within expressions using backticks, either. The person who has to read your code in six months (and that might be you) will love you for abandoning the backticks.

HTH...
 
1 members found this post helpful.
Old 08-31-2019, 12:07 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
If portability matters, use $(( )) that is a (arithmetic) expression and works in all Posix-compatible shells.
In contrast, the (( )) is a (arithmetic) command, and only works in bash, ksh, zsh.
With $(( )) there is a one-to-one translation from expr
Code:
count=`expr $count + 1`
translates to
Code:
count=$(($count + 1))
expr is itself a command, its arguments are expanded by the shell unless \escaped
Code:
mult=`expr $count \* 2`
translates to
Code:
mult=$(($count * 2))
More advanced stuff:
Within $(( )) you can omit the $ in front of variables, because any letter-word MUST be a variable. Also spaces can be omitted.
Code:
count=$((count+1))
But the $ is still required in case you use variable modifiers, for example
Code:
count=$((${count:-0} + 1))
I guess the (( )) will make it into the next Posix standard... whenever that will be.

Last edited by MadeInGermany; 08-31-2019 at 12:19 PM.
 
2 members found this post helpful.
  


Reply

Tags
expr



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggestion for posts made in another language besides English... ardvark71 LQ Suggestions & Feedback 5 05-02-2015 06:25 PM
Another camera import utility besides Digikam? SpelledJ Linux - Software 3 08-03-2010 04:58 AM
[SOLVED] test expr VS [ expr ] ashok.g Programming 3 12-03-2009 11:15 PM
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 05:31 PM
perl script reg expr confusion rajatgarg Programming 1 04-09-2004 03:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration