Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

about C Shell Script !!

Posted on 2004-09-03
3
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

618 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