Sponsored Content
Top Forums Shell Programming and Scripting Howto shorten script in a busybox environment by using for loops? Post 302399054 by Nila on Friday 26th of February 2010 06:33:22 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
CAL(1)							    BSD General Commands Manual 						    CAL(1)

NAME
cal, ncal -- displays a calendar and the date of Easter SYNOPSIS
cal [-3hjy] [-A number] [-B number] [[month] year] cal [-3hj] [-A number] [-B number] -m month [year] ncal [-3hjJpwy] [-A number] [-B number] [-s country_code] [[month] year] ncal [-3hJeo] [-A number] [-B number] [year] ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm] DESCRIPTION
The cal utility displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of Easter. The new format is a little cramped but it makes a year fit on a 25x80 terminal. If arguments are not specified, the current month is dis- played. The options are as follows: -h Turns off highlighting of today. -J Display Julian Calendar, if combined with the -e option, display date of Easter according to the Julian Calendar. -e Display date of Easter (for western churches). -j Display Julian days (days one-based, numbered from January 1). -m month Display the specified month. If month is specified as a decimal number, it may be followed by the letter 'f' or 'p' to indicate the following or preceding month of that number, respectively. -o Display date of Orthodox Easter (Greek and Russian Orthodox Churches). -p Print the country codes and switching days from Julian to Gregorian Calendar as they are assumed by ncal. The country code as deter- mined from the local environment is marked with an asterisk. -s country_code Assume the switch from Julian to Gregorian Calendar at the date associated with the country_code. If not specified, ncal tries to guess the switch date from the local environment or falls back to September 2, 1752. This was when Great Britain and her colonies switched to the Gregorian Calendar. -w Print the number of the week below each week column. -y Display a calendar for the specified year. -3 Display the previous, current and next month surrounding today. -A number Display the number of months after the current month. -B number Display the number of months before the current month. -C Switch to cal mode. -N Switch to ncal mode. -d yyyy-mm Use yyyy-mm as the current date (for debugging of date selection). -H yyyy-mm-dd Use yyyy-mm-dd as the current date (for debugging of highlighting). A single parameter specifies the year (1-9999) to be displayed; note the year must be fully specified: ``cal 89'' will not display a calendar for 1989. Two parameters denote the month and year; the month is either a number between 1 and 12, or a full or abbreviated name as speci- fied by the current locale. Month and year default to those of the current system clock and time zone (so ``cal -m 8'' will display a calen- dar for the month of August in the current year). Not all options can be used together. For example ``-3 -A 2 -B 3 -y -m 7'' would mean: show me the three months around the seventh month, three before that, two after that and the whole year. ncal will warn about these combinations. A year starts on January 1. Highlighting of dates is disabled if stdout is not a tty. SEE ALSO
calendar(3), strftime(3) HISTORY
A cal command appeared in Version 5 AT&T UNIX. The ncal command appeared in FreeBSD 2.2.6. AUTHORS
The ncal command and manual were written by Wolfgang Helbig <helbig@FreeBSD.org>. BUGS
The assignment of Julian-Gregorian switching dates to country codes is historically naive for many countries. Not all options are compatible and using them in different orders will give varying results. BSD
March 14, 2009 BSD
All times are GMT -4. The time now is 03:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy