WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: an elementary difficulty with bash scripting  (Read 4861 times)

Offline shail.dw

  • WikiUser
  • *
  • Posts: 34
an elementary difficulty with bash scripting
« on: April 04, 2012, 12:55:25 PM »
1) Only yesterday I tried to do my first Bash scripting with Tiny core v. 4.3. Even with such a simple for loop like the following:

#!/bin/sh
for ((i=0; i<=100; i++))
do
echo "$i, "
done

This returns me with the error "line 2: syntax error: bad for loop variable". I can not understand. I am also new to scripting. Please someone may set me go ahead.

2) How can I find what Bash version is in use on Tiny core. Bash --Ver, Bash --version, sh --version etc. has not helped me in no way.

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: an elementary difficulty with bash scripting
« Reply #1 on: April 04, 2012, 01:07:26 PM »
/bin/sh is ash, not bash.
If you really need bash, you must install the bash extension and use

#!/usr/local/bin/bash

Offline shail.dw

  • WikiUser
  • *
  • Posts: 34
Re: an elementary difficulty with bash scripting
« Reply #2 on: April 04, 2012, 01:26:31 PM »
That suffices me.

I also remember you as the first respondent to any of my posts on this forum. Thanx again.

Offline maro

  • Hero Member
  • *****
  • Posts: 1228
Re: an elementary difficulty with bash scripting
« Reply #3 on: April 04, 2012, 02:58:47 PM »
Regarding your second question: I'm not sure what went wrong at your end, because it works for me as expected:
Code: [Select]
tc@box:~$ bash --version
GNU bash, version 4.0.33(1)-release (i686-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
tc@box:~$

BTW, I've come to appreciate the fact that TC comes out of the box just with 'ash', as it encourages me to avoid any Bashisms. For me this is a better practice as it increases the chances that a shell script created for TC works without change in a different environment that might not have a BASH (or uses a different shell, e.g. 'ksh' in the case of UWIN or Solaris).

Furthermore, apart from the alternative shown in the page referenced above (i.e. a 'while' loop with a variable increment inside the loop) you could use for i in $( seq 0 100 ) as a replacement for your first line in your code snippet. This would be another way to avoid the need to install 'bash.tcz'.

Offline shail.dw

  • WikiUser
  • *
  • Posts: 34
Re: an elementary difficulty with bash scripting
« Reply #4 on: April 05, 2012, 02:32:50 AM »
@maro:

1) My question clearly indicated that I was unaware of the existence of bash on my computer, which gerald_clark so nicely caught me. He even guided me to change the first line of my script, otherwise I would be returning here complaining once more. So with bash lacking on my computer, there was no question of getting its version. Once bash.tcz was 'installed", bash --version on my computer gives same results as it gives on your side. It is OK.

2) Thanks for the alternate for loop you have suggested me. At this stage of learning I can appreciate everything said to me. Also I am reading articles on shell scripting.

3) Since I am new to scripting at all, so would you or others really strongly suggest me that I take ash rather than bash ? I have just begun, and it is easy to change the shell, rather than it may be late later on. Or are the differences meagre, and can be overcome with ease even later on ?

4) What is the guarantee every system will have ash ? Some may be having bash. II will read about "Bashisms" later on.

Thanks all.

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10217
Re: an elementary difficulty with bash scripting
« Reply #5 on: April 05, 2012, 03:00:16 AM »
If you write your scripts for ash, it's very likely they will work on most shells (bash, dash, ksh...).
The only barriers that can stop you are the ones you create yourself.

Offline shail.dw

  • WikiUser
  • *
  • Posts: 34
Re: an elementary difficulty with bash scripting
« Reply #6 on: April 06, 2012, 07:14:54 AM »
Thanks curaga. Though presently occupied with Bash, I will go learning ash too, as well as that I would practice ash when it comes to writing something useful. I got the message.