How to write script in bash.


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions How to write script in bash.
# 1  
Old 08-02-2010
Question How to write script in bash.

I am very new to Linux/Unix. Kindly assist the following:

I wish to write a bash shell script called how_many_to_go that calculates and prints the number of days, hours, minutes and/or seconds until the end of the current month (based on the output of the date command).

  • Do not make any assumptions about what month it is - the script should run properly in any month.
  • Assume that it is not a leap year, so the number of days in any month is known (February has 28 days; April, June, September, and November have 30 days; All the rest have 31 days).
  • The output should read "There are d days, h hours, m minutes, and s seconds until the end of the month" except as modified by the command line arguments below.
  • The script should accept command line arguments as follows (assume that it's now 11:00:00 PM on January 30):
    • how_many_to_go - print the number of days, hours, minutes, and seconds until the end of the month (e.g. "There are 1 days, 1 hours, 0 minutes, and 0 seconds until the end of the month")
    • how_many_to_go h - print the number of hours, minutes, and seconds until the end of the month (e.g. "There are 25 hours, 0 minutes, and 0 seconds until the end of the month")
    • how_many_to_go m- print the number of minutes and seconds until the end of the month (e.g. "There are 1500 minutes and 0 seconds until the end of the month")
    • how_many_to_go s - print the number of seconds until the end of the month (e.g. "There are 90000 seconds until the end of the month")


Kenyatta University Kenya. Lecturer: Dr. Waweru Michael. Course Code. BBIT 202.

Regards
# 2  
Old 08-02-2010
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
# 3  
Old 08-02-2010
Hi alobi,
Here is the script -

num=$#
option=$1
typeset -R2 lastday=$(cal)
currdate=`date "+%d"`
days_to_go=`expr $lastday - $currdate`
hrs_gone=`date "+%H"`
mins_gone=`date "+%M"`
secs_gone=`date "+%S"`
secs_gone=`expr $secs_gone + $mins_gone \* 60`
secs_gone=`expr $secs_gone + $hrs_gone \* 60 \* 60`
secs_to_go=`expr $days_to_go \* 24 \* 60 \* 60`
secs_to_go=`expr $secs_to_go - $secs_gone`
if [ $num -gt 0 ]
then
if [ $option == "-s" ]
then
echo "Time left is 0 : 0 : 0 : $secs_to_go"
elif [ $option == "-m" ]
then
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
echo "Time left is 0 : 0 : $mins : $secs"
elif [ $option == "-h" ]
then
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
hrs=`expr $mins / 60`
mins=`expr $mins % 60`
echo "Time left is 0 : $hrs : $mins : $secs"
fi
else
secs=`expr $secs_to_go % 60`
mins=`expr $secs_to_go / 60`
hrs=`expr $mins / 60`
mins=`expr $mins % 60`
days=`expr $hrs / 24`
hrs=`expr $hrs % 24`
echo "Time left is $days : $hrs : $mins : $secs"
fi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. UNIX for Beginners Questions & Answers

How To Write my Bash Script To Automate it?

Hello guys, I need some help. I am new in bash and I don't know how to automate the following script. head -2 out1 > 1.fasta sed ‘1,2 d' out1 > out2 rm out1 head -2 out2 > 2.fasta sed ‘1,2 d' out2 > out1 rm out2 head -2 out2 > 3.fasta sed '1,2 d' out2 > out1 rm out2 .......... (3 Replies)
Discussion started by: dellia222
3 Replies

3. UNIX for Dummies Questions & Answers

Script to break up file (write new files) in bash

Hello experts, I need help writing individual files from a data matrix, with each new file being written every time there is a blank line: From this cat file.txt col1 col2 col3 6661 7771 8881 6661 7771 8881 6661 7771 8881 col1 col2 col3 3451 2221 1221... (6 Replies)
Discussion started by: torchij
6 Replies

4. Shell Programming and Scripting

How to write bash script for lvm snapshot?

Hi Team I am trying to put together a nice small script to mount my lvm snapshot Here are my objectives 1 Check whether snapshot is currently mounted. If so echo umount snapshot and exit from the script. 2 If it's not mounting, check whether the mount point exist, If so, create lvm... (0 Replies)
Discussion started by: fugeulu
0 Replies

5. Shell Programming and Scripting

How to write bash shell script for mentioned requirement?

Hi All, I am unable to write the script for the below requirement. Requirement: Main table dir_ancillary table contain three column "dir_ancillary.table_name"," dir_ancillary.text_file_name ", "dir_ancillary.Include" . This dir_ancillary contain undefined tables in the column... (2 Replies)
Discussion started by: Vineeta Nigam
2 Replies

6. Shell Programming and Scripting

how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download. i writed this script #!/bin/bash wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master CURRENDIR=/home/kernel-downloader cd $CURRENDIR rm $CURRENDIR/zipfiles 2>/dev/null ... (2 Replies)
Discussion started by: ron gokhle
2 Replies

7. Shell Programming and Scripting

Bash script reg-exp , replace , open and write

Hi All I am a new in scripting language and I would like help for you guys I would like to create a file named constant.h and search into all files *.m in specific directory for a reg-exp @"LBL_]+" exp: @"LBL_75847" , and write those matchs to constant.h if there are not written (no... (15 Replies)
Discussion started by: molwiko
15 Replies

8. Shell Programming and Scripting

Help to write bash script code

I am newbie to UNIX. I came across this exercise in one the books.Can anyone help me with this question?????? Write a short Bash script that, given the name of a file as an argument, reads the file name and creates a new file containing only lines which consist of one word. Here is an example... (4 Replies)
Discussion started by: krthknaidu
4 Replies

9. Shell Programming and Scripting

Bash Script to Read & Write on different directories

Hi, root@server] df -h 121G 14G 101G 12% /home 147G 126G 14G 91% /backup We having our site files and images are storing in /backup/home/user/files/ through symbolic link created in /home directory pointing in /backup directory as following. root@server] cd /home... (1 Reply)
Discussion started by: mirfan
1 Replies

10. Shell Programming and Scripting

Can anybody write this bash script ?

hi, first congratulations on the nice forum! Can anybody write script, which can make copy of some or all files of the current directory in new directory (called "backups", which must be made in the current directory, if it's not already exist). And bring out a massage (report) with the count... (7 Replies)
Discussion started by: Cecko
7 Replies
Login or Register to Ask a Question