Solved

about C Shell Script !!

Posted on 2004-09-03
3
211 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now