?
Solved

about C Shell Script !!

Posted on 2004-09-03
3
Medium Priority
?
248 Views
Last Modified: 2010-04-15
Hi Experts,

I want to write a C Shell Script called 'convertsec' which read in a number (in second) and print out the number of the minutes and also needed to check the negative number input ! Can anyone helps ??

This shell script is used in Unix environment for experimental use !! thanks !!

Mike
0
Comment
Question by:mmccy
3 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11978510
the following should get you started

#! /bin/csh -f
@ a = $1 / 2;
echo $a;

the above script will take an argument and divide the argument by 2

you can save the above script in a file test.csh and chmod +x test.csh
and run it as

./test.csh 3
./test.csh 5

etc etc


though be careful : csh can only do integer arithmetic
and that there are reasons not to code in csh


Csh Programming Considered Harmful http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/



0
 

Expert Comment

by:khom
ID: 11983619
Hi Mike,
      Place the following code in a file called "convertsec.sh". Execute the command "chmod +x convertsec.sh". Run the script by executing "convertsec.sh <seconds>". It will accept positive and negative values for the number of seconds. You can format the output to suit your needs.

#!/bin/sh
if [ $# -eq 0 ]
then
        echo "Usage: convertsec.sh <seconds>"
        exit 1
fi
a=`expr $1 "/" 60`
b=`expr $a "*" 60`
b=`expr $1 "-" $b`
echo "$a minutes $b seconds"
exit 0
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 2000 total points
ID: 11994603
Do you want a C shell script or a C program? If you were looking for a csh script, then this question should have been placed in the unix programming TA. This TA is for C programming language. Both are two different worlds :)

Since this is obviously a learner's assignment, I will provide you some hints for the script, rather a semi-complete script, and you should be able to write it on your own in no time

To add the negative number checks to khom's script

#!/bin/sh
if [ $# -eq 0 ]
then
        echo "Usage: convertsec.sh <seconds>"
        exit 1
fi

if [ $1 -lt 0 ]
then
        echo negative number was given as agument
        exit 1
fi

a=`expr $1 "/" 60`
b=`expr $a "*" 60`
b=`expr $1 "-" $b`
echo "$a minutes $b seconds"
exit 0

This script will work wish sh or bash but not execute correctly in csh

Here is a csh script which does the same ... This is partial and you should be able to write what you are looking for

#!/bin/csh

if ( ${#argv} == 0 ) then    --->> the expression ${#argv} refers to the number of arguments ... here we are checking only the case when an argument is not supplied at all ... It will accept 2 or more arguments too ... perhaps you would like to add check for that and modify the error message to show usage
     echo missing argument
     exit 1
endif

if ( $argv[1] < 0 ) then   --> argv is an array and [1] is the subscript of the array refering to the first argument
     echo negative argument
     exit 1
endif

@ min = $argv[1] / 60    --> simple calculations to get minutes and seconds ...

@ sec =   ->> fill in these two lines

@ sec =

echo $min minutes $sec seconds ->> display the results

cheers
sunnycoder
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question