LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-04-2013, 02:57 PM   #16
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 311

Rep: Reputation: 120Reputation: 120

Quote:
Originally Posted by konsolebox View Post
Can I ask in what sense could it be more portable? What shell / version of Bash?
Portable if anyone decides to port the script to a pure POSIX shell.
 
Old 02-05-2013, 09:43 PM   #17
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,141

Rep: Reputation: 329Reputation: 329Reputation: 329Reputation: 329
With a fairly minor change (in red, below) ntubski's test program works fine:
Code:
$ cat greatest
#!/bin/bash

# run this in a new directory
mkdir ./gr
pushd gr &>/dev/null
touch 000{1..8}.mp3

files=(*.mp3)
greatest=$(basename ${files[-1]%.*})
((i=10#$greatest+3))
echo "i = $i, greatest = $greatest"
popd &>/dev/null
rm -rf gr/
$ ./greatest 
i = 11, greatest = 0008
<edit>
Of course, adding the 3 is unnecessary if you explicitly set your base to ten...
</edit>

Last edited by PTrenholme; 02-05-2013 at 09:48 PM.
 
Old 02-05-2013, 10:01 PM   #18
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,307

Rep: Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767Reputation: 767
Quote:
Originally Posted by mina86 View Post
Portable if anyone decides to port the script to a pure POSIX shell.
In which case arithmetic expressions wouldn't work at all, no?

@PTrenholme: yes, but the question is really how the original code from the first post (supposedly) worked. My guess is that jroggow didn't actually run the code they thought they were running.
 
Old 02-06-2013, 10:50 AM   #19
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 311

Rep: Reputation: 120Reputation: 120
Quote:
Originally Posted by ntubski View Post
In which case arithmetic expressions wouldn't work at all, no?
Of course they would. But yes, I was a bit haste in my explanation. The $(($x)) meaning the same as $((x)) statement was added in later POSIX versions as well, so a recent pure POSIX shell will also support it, but nonetheless, using dollar sign helps with portability with those slightly older pure POSIX shells.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IP with padded zeroes in bash frater Linux - Software 7 10-29-2010 08:59 AM
in copy files or ls files the command want to invert select some files how to?? hocheetiong Linux - Newbie 3 06-27-2008 06:32 AM
payload is padded with zeros ahm_irf Linux - Networking 1 11-11-2007 06:07 AM
Date command - month of year, blank padded? menator Programming 3 06-27-2006 06:00 AM
How do I print using format that padded space between printed value ? Linh Programming 2 06-18-2004 03:22 PM


All times are GMT -5. The time now is 07:42 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration