LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 02-04-2013, 02:57 PM   #16
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229

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,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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



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
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:01 PM.

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