Ash Documentation?

Dallas Clement dallas.a.clement at gmail.com
Wed Jun 27 10:18:50 UTC 2007


On Wed, 2007-06-27 at 11:08 -0400, Paul Smith wrote:
> On Wed, 2007-06-27 at 05:02 -0500, Dallas Clement wrote:
> > Can anyone please suggest a reference to some good Ash scripting
> > documentation?  I am trying to implement scripting like I'm using
> > Bourne shell and things are not behaving too well...
> 
> Most likely you're actually using bash syntax, not Bourne shell syntax.
> Bash has a number of enhancements that are not supported by Bourne shell
> (or the POSIX sh standard).  However, because many GNU/Linux
> distributions come with bash as /bin/sh, people who are learning shell
> scripting on GNU/Linux often make the mistake of thinking bash features
> are Bourne shell features.
> 
> > In particular, I'm wondering how to correctly define shell script
> > functions and call the functions.  I presume this is supported with
> > the busybox ash implementation.
> 
> Yes, functions are supported.
> 
> Since you don't give an example of what you're doing, we'll just have to
> guess: I guess you're doing this:
> 
>         function myfunc () {
>         	...
>         }
> 
> This is not valid Bourne/POSIX sh syntax.  The "function" keyword is a
> bash-ism, and is not needed in Bourne/POSIX sh.  Just write:
> 
>         myfunc () {
>         	...
>         }
> 
> If this isn't the problem, please provide a sample of the code you're
> using and show us what errors you get when you try it.
> 
> Cheers!
> 

Apologies for the rather length example of what I'm doing below.  I'm
basically invoking this ash script from within initramfs.  It's an
install script.  What I've noticed is that the echo statements within my
functions are not being seen on the console.  So I can't tell whether
they are being entered or not.  Neither can I see any output from other
commands such as "fdisk".  Hence, not sure if I'm using correct syntax
or doing something else wrong.

Thanks for the help!

----------------------------

determine_boot_drive_name()
{
   echo "Entering determine_boot_drive_name"

   local dev80_dir=/sys/firmware/edd/int13_dev80
   local heads_file=/sys/firmware/edd/int13_dev80/default_heads
   local
spt_file=/sys/firmware/edd/int13_dev80/default_sectors_per_track
   local cylinders_file=/sys/firmware/edd/int13_dev80/default_cylinders

   if [ -d $dev80_dir ]; then
      if [ -f $heads_file ]; then
         if [ -f $spt_file ]; then
            if [ -f $cylinders_file ]; then
               num_bios_drive_heads=$(cat $heads_file)
               num_bios_drive_sectors_per_track=$(cat $spt_file)
               num_bios_drive_cylinders=$(cat $cylinders_file)

               export num_bios_drive_heads
               export num_bios_drive_sectors_per_track
               export num_bios_drive_cylinders

               local num_disk_lines=$(fdisk -l | grep "Disk" | wc -l)
               local num_heads_lines=$(fdisk -l | grep "heads" | wc -l)

               if [ $num_disk_lines -eq $num_heads_lines ]; then
                  if [ $num_disk_lines -gt 0 ]; then
                     line_num=$(fdisk -l | awk '/heads/ \
                        {if ($1 == ENVIRON["num_bios_drive_heads"] && \
                             $3 ==
ENVIRON["num_bios_drive_sectors_per_track"] && \
                             $5 == ENVIRON["num_bios_drive_cylinders"])
{print NR; exit;}}')
                     export line_num
                     boot_drive_name=$(cat $drive_names_tmp_file | \
                        awk '{if (NR == ENVIRON["line_num"]) {print
substr($2,0,8); exit}}')
                     return 0
                  else
                     echo "Unable to detect boot drive using fdisk"
                  fi
               else
                  echo "Unable to detect boot drive using fdisk; names
and params don't match."
               fi
            else
               echo "Couldn't find boot drive BIOS EDD cylinders file."
            fi
         else
            echo "Couldn't find boot drive BIOS EDD sectors file."
         fi
      else
         echo "Couldn't find boot drive BIOS EDD heads file."
      fi
   else
      echo "Couldn't find boot drive BIOS EDD directory."
   fi

   return 1
}

create_device_files()
{
   echo "Entering create_device_files"

   local device_tmp_file=/var/tmp/device_tmp_file
   local partitions_file=/proc/partitions

   if [ -f $partitions_file ]; then
      cat $partitions_file | awk '$1 ~ /[0-9]/ {print "mknod -m
660 /dev/" \
      $4, "b", $1, $2}' > $device_tmp_file
      cat $device_tmp_file
      . $device_tmp_file
      rm -f $device_tmp_file
   else
      echo "Partitions file not found"
   fi
   return 0
}

foobar()
{
   echo "Entering foobar"

   echo $boot_drive_name

   if [ -n $boot_drive_name ]; then
#      fdisk -l | grep "Disk" | grep -v $boot_drive_name | \
#      awk '/Disk/ {print NR ".", $0}' > $drive_names_tmp_file
#      cat $drive_names_tmp_file
      fdisk -l
      return 0
   else
      echo "Boot drive name is unknown"
   fi

   return 1
}

main()
{
   echo "Entering main"

   mount -t sysfs none /sys
   mount -t proc none /proc

   ./load-modules.ash

   if [ create_device_files ]; then
      if [ determine_boot_drive_name ]; then
         if [ -n $boot_drive_name ]; then
            boot_part_name=$(fdisk -l | grep ^"/dev" | cut -d ' ' -f1 |
\
            awk 'substr($1,0,7) == ENVIRON["boot_drive_name"] {print
substr($1,0,7); exit;}')
            if [ -n $boot_part_name ]; then
               echo $boot_part_name
               foobar
               return 0   
            else
               echo "Unable to determine boot partition"
            fi
         else
            echo "Empty boot drive name string"
         fi
      else
         echo "Caught error while determining boot drive name"
      fi
   else
      echo "Could not create device files"
   fi
   return 1
}

if [ main ]; then
   echo "Exited main successfully"
   exec /bin/ash </dev/tty0 > /dev/tty0 2>&1
else
   echo "Aborting installation..."
   exit 1
fi





More information about the busybox mailing list