• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

about C Shell Script !!

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
mmccy
Asked:
mmccy
1 Solution
 
avizitCommented:
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
 
khomCommented:
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
 
sunnycoderCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now