Visit Our UNIX and Linux User Community


Howto shorten script in a busybox environment by using for loops?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Howto shorten script in a busybox environment by using for loops?
# 1  
Old 02-26-2010
Howto shorten script in a busybox environment by using for loops?

My satellite receiver is equipped with busybox, so a small linux version.
That is why I can not use certain commands like #tomorrow in date commands or #date -d "+1 day"
and thus I have to use: day1=$[$day0+1]

I want to download every day 6 files from the internet but the filenames consist of the date of today and the next five days.

I have written a bash script which does what I want. However it is straight written and can be smaller I think. I do not know how to calculate with variables in a for loop. Below is a part of the script I wrote and in the script

Code:
day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            # 
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

day1=$[$day0+1]                # day1 = day of today + 1 so tomorrows date
day2=$[$day1+1]                # day2 = day of day1 + 1
day3=$[$day2+1]                # day3 = day of day2 + 1
day4=$[$day3+1]                # day4 = day of day3 + 1
day5=$[$day4+1]                # day5 = day of day4 + 1

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day1               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day1=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
FILE1=$year$month$day1"_US.gif"        # real filename for FILE1 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day2               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day2=`printf "%02d" $day_temp`        # day2 = daytemp but with leading zero
FILE2=$year$month$day2"_US.gif"        # real filename for FILE2 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day3            # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day3=`printf "%02d" $day_temp`        # day3 = daytemp but with leading zero
FILE3=$year$month$day3"_US.gif"        # real filename for FILE3 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day4            # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day4=`printf "%02d" $day_temp`        # day4 = daytemp but with leading zero
FILE4=$year$month$day4"_US.gif"        # real filename for FILE4 download

if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=$day5               # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day5=`printf "%02d" $day_temp`        # day5 = daytemp but with leading zero
FILE5=$year$month$day5"_US.gif"        # real filename for FILE5 download

In stead of day1=... day2=.... day3=... etc, I started looking for something like (but did not functionate)
Code:
day0=`date +"%e"`
for ((i=1; i<6; i++)); 
do
   day$i=$[$day$(($i-1)) + 1]      # find the value of the previous day and add that value with one
done

Who can help me by shorten the script?

Last edited by ni_hao; 02-26-2010 at 07:21 AM..
# 2  
Old 02-26-2010
I replaced lot of statements in your script by using for loop.

The script is as follows.

Code:
day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
day_temp=$day$i              # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
day$i=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
FILE$i=$year$month$day$i"_US.gif"        # real filename for FILE1 download
done

# 3  
Old 02-26-2010
Thanks, looks much much better. However, some commands are not recognized at the busybox I use. I tried your source code and it gave errors:

This is not recognized:

Code:
  day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date

and returns with

Code:
day1=201002261: command not found

So it did not add 1 day to today but put the 1 behind the date of today.
With other words: it should give 27 but it gave: 201002261

In that construction $day$i is seen as two variables: $day and $i that is why the result is: 20100226 and 1; combined to 201002261

Last edited by ni_hao; 02-26-2010 at 07:54 AM..
# 4  
Old 02-26-2010
Sorry.Now you check the following code.I corrected your errors.

Code:
day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day0=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
let day$j=$[$day$i+1]                # day1 = day of today + 1 so tomorrows date
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
day_temp=$day$i              # day_temp will be used in check_end_of_month_year
fi
check_end_of_month_year            # check if it will be end of month/year
let day$i=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero
if [[ $i -eq 1 ]];then
FILE1=$year$month$day$i"_US.gif"        # real filename for FILE1 download
elif [[ $i -eq 2 ]];then
FILE2=$year$month$day$i"_US.gif"        # real filename for FILE2 download
elif [[ $i -eq 3 ]];then
FILE3=$year$month$day$i"_US.gif"        # real filename for FILE3 download
elif [[ $i -eq 4 ]];then
FILE4=$year$month$day$i"_US.gif"        # real filename for FILE4 download
elif [[ $i -eq 5 ]];then
FILE5=$year$month$day$i"_US.gif"        # real filename for FILE5 download
fi
done

# 5  
Old 02-26-2010
Thanks again but same error happens now with the FILE$i variable.

This
Code:
FILE1=$year$month$day$i"_US.gif"

give as result:
Code:
201003201002261_US.gif

and should be
Code:
20100227_US.gif

So again, the $day$i is not recognized well and also let day$j=$[$day$i+1] is not recognized as it should

Last edited by ni_hao; 02-26-2010 at 11:09 AM..
# 6  
Old 02-27-2010
Sorry.There is some problem in interpolation of the variables.

Now,I corrected those errors.You check the following code.

Code:
day=`date +"%Y%m%d"`
month=`date +"%m"`
year=`date +"%Y"`

days_in_month()                # maximum days of every month
{
  case $month in
    09|04|06|11) maxdays=30 ;;
    01|03|05|07|08|10|12) maxdays=31 ;;
    02) if (( ("$year" % 400) == "0" )) || (( ("$year" % 4 == "0") && ("$year" % 100 != "0") )) ; then
          maxdays=29
        else
          maxdays=28
       fi
  esac
}

check_end_of_month_year()        # check if it will be end of month/year
{
  if [ $day_temp -gt $maxdays ]; then # reached end of month
    day_temp=01
    month=$[$month+1]
    if [ $month -gt 12 ]; then       # reached and of year
      month=1
      year=$[$year+1]
    fi
    new_month=yes            #
    days_in_month            # calculate max days in new month
  fi
  month=`printf "%02d" $month`
}

days_in_month                   # how many days does current month have
new_month=no                  # parameter to indicate new month or not
day[0]=`date +"%e"`            # put date of today in day0
FILE0=$day"_US.gif"            # real filename for FILE0 download

# ######################################
# ------- I think the source below can be smaller -------
# ######################################

j=1
for((i=0;i<5;i++))
do
let day[$j]=$[${day[$i]}+1]
let j=j+1
done

for((i=1;i<6;i++))
do
if [ $new_month = yes ]; then
  day_temp=$[$day_temp+1]        # day_temp will be used in check_end_of_month_year
else
  day_temp=${day[$i]}
fi
check_end_of_month_year            # check if it will be end of month/year
day[$i]=`printf "%02d" $day_temp`        # day1 = daytemp but with leading zero

#echo ${day[$i]}

if [[ $i -eq 1 ]];then
FILE1=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE1
elif [[ $i -eq 2 ]];then
FILE2=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE2
elif [[ $i -eq 3 ]];then
FILE3=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE3
elif [[ $i -eq 4 ]];then
FILE4=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE4
elif [[ $i -eq 5 ]];then
FILE5=$year$month${day[$i]}"_US.gif"        # real filename for FILE1 download
#echo $FILE5
fi
done

# 7  
Old 02-27-2010
Wow, thanks a lot it works. I just have changed

Code:
  else
    day_temp=${day[$i]}
  fi

into
Code:
  else
    day_temp=${day[$i]: -2}        # day_temp is filled with the last 2 characters of ${day[$i]}
  fi


Previous Thread | Next Thread
Test Your Knowledge in Computers #107
Difficulty: Easy
Each Unix kernel subsystem has key features like concurrency, virtual memory, paging, and a virtual file system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Howto stop loops in CentOS

Good morning, At the client location os is CentOS. In all the terminals i.e F1, F2, F3....F10 PING command is continuously running. I tried to terminate it using CTRL C or quit but unable to stop that command in all the terminals. How to stop that? Howto find batch files which are being... (3 Replies)
Discussion started by: sureshbabu.anis
3 Replies

2. Shell Programming and Scripting

Ps command and awk - shorten characters

ps -e -o pcpu -o pid -o user -o args | sort -k 1 | tail -6r %CPU PID USER COMMAND 0.3 223220 root /usr/tivoli/tsm/client/ba/bin/dsmc sched 0.2 411332 root /usr/sbin/syslogd 0.1 90962 root /usr/sbin/syncd 60 0.0 10572 root -ksh 0.0 94270 root -ksh ... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. UNIX for Dummies Questions & Answers

How can i use function for the below script to shorten it?

Hi All, i worte a shell script which will zcat the .gz file and write it in to a tmp file and then again cat the file and convert it to Dos mode. Next step is i am greping the file to search for the particular string on the 1st line and if the string does not exits it will insert the 1st line... (1 Reply)
Discussion started by: vikatakavi
1 Replies

4. UNIX for Dummies Questions & Answers

How to shorten my code?

salary_range_report() { echo -e ${underline}$redYellow"\nSalary Range Report\n" tput sgr0 count_0_to_999=0 count_1000_to_2999=0 count_2000_to_5999=0 count_6000_to_9999=0 count_10000_above=0 for i in `cut -d "," -f4 $PAYROLL` #Loop Salary do if && then let... (4 Replies)
Discussion started by: eggisbad
4 Replies

5. Shell Programming and Scripting

Bash Script to Ash (busybox) - Beginner

Hi All, I have a script that I wrote on a bash shell, I use it to sort files from a directory into various other directories. I have an variable set, which is an array of strings, I then check each file against the array and if it is in there the script sorts it into the correct folder. But... (5 Replies)
Discussion started by: sgtbobie
5 Replies

6. Shell Programming and Scripting

Script loops again and again and again and ...

Hi, Linux newbie here with what I'm guessing is silly questions. My script below is working in that it correctly copies files from the backup IP (10.0.91.1) back down to the Linux server but trouble is it loops continuously. It correctly downloads 100 files from the the IP 10.0.91.1... (1 Reply)
Discussion started by: MOWS
1 Replies

7. Shell Programming and Scripting

Need help optimizing this piece of code (Shell script Busybox)

I am looking for suggestions on how I could possibly optimized that piece of code where most of the time is spend on this script. In a nutshell this is a script that creates an xml file(s) based on certain criteria that will be used by a movie jukebox. Example of data: $SORTEDTMP= it is a... (16 Replies)
Discussion started by: snappy46
16 Replies

8. Shell Programming and Scripting

script to shorten usernames and output to file

Hopefully someone here can point me in the correct direction. I'm working on a username migration and am trying to map my users ols usernames to the new ones. Right now every user has a username of firstname.lastname i.e. john.doe I'm trying to create a bash or python script that will take... (3 Replies)
Discussion started by: binary-ninja
3 Replies

9. Shell Programming and Scripting

howto run remotely call function from within script

Hi I have the following script : #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` } ####################################################################### # # print... (2 Replies)
Discussion started by: presul
2 Replies

10. Shell Programming and Scripting

Howto read data into a shell script

Hi all, I'm anew shell user, and want to now how to do that? i have a files that have inside few raws of names of directories, i want to read only the lins that are a dir name how to do so. the file looks like that #################################### # This is the list of dirs files ... (4 Replies)
Discussion started by: banjo
4 Replies

Featured Tech Videos