CS-Reloaded Banner
Welcome, Guest. Please Login or Register.
Download Second Map Pack!
Get more packs here

Home Help Search Login Register

CSReloaded Forums  |  General Category  |  Help & Troubleshooting (Moderator: Guardian_Tenshi)  |  Topic: *nix command line args
Pages: [1] Reply Notify of replies
   Author  Topic: *nix command line args  (Read 95 times)
Guardian_Tenshi
Global Moderator
*****
Karma: +53/-26

Offline

Gender: Male
Posts: 1114

276733708 276733708 clanguardian2003 Ol+Grimmy
View Profile WWW E-Mail
*nix command line args
« on: March 19, 2004, 10:05:40 PM »
Reply with quote

Ok, so our assignment is to write a c-shell script file, and not a very complex one...but there is one thing that is still alluding me.  She wants to pass it arguments.  For example, in this assigment, she wants it to be called "timer" and then timer is called:
% timer 9000
the script would then do a simple conversion and convert 9000 seconds into 2 hours and 30 minutes.  However, we aren't allowed to use anything but integers.  So...I understand how to validate the number of args, I understand how to prompt the user should he/she not provide enough args...what I DON'T understand, is the logic to prove if the number is a valid integer.  That means it can't be a float, double, or string...think someone can help??  <<looks at porter, slight, and terraji>>

Tenshi
Logged

Grounded
Admin Team
CSR Connoisseur
*****
Karma: +158/-21

Offline

Posts: 3176


View Profile
Re:*nix command line args
« Reply #1 on: March 20, 2004, 04:33:39 AM »
Reply with quote

Do you have commands that let you do modulo arithmetic? I'm guessing you do, in which case the number would be valid if it is equal to 0 mod 30 (assuming you're only supposed to convert if the number is a multiple of 30 - you could go to 15 for quarter hours).
Logged
Terraji
Admin Team
CSR Connoisseur
*****
Karma: +35/-15

Offline

Gender: Male
Posts: 789

terraji@hotmail.com
View Profile E-Mail
Re:*nix command line args
« Reply #2 on: March 20, 2004, 01:25:21 PM »
Reply with quote

It is my understanding that args always show up in your C program as strings.

To validate whether it is not a word, float or double etc, you could scan through the arg string once and make sure that each char is between 48 and 57 (ascii 0-9) when you cast them to an int. If there is a period (decimal place) or a letter the string would be rejected. Once you have a verified string you can use the
Code:
int atoi(const char * nptr);
function to convert it to a integer. I dont think you have to include a special library to use the atoi function, if not, it is probably in the string.h standard library.

One thing to watch out for which has messed me up countless times, is that for the last command line arg, it includes the newline character from when you pressed enter to execute the program on the command line. For example, %timer 9000 would have arg[1] string as ['9','0','0','0','\n',null]

I hope this helps 

Terraji
Logged
Guardian_Tenshi
Global Moderator
*****
Karma: +53/-26

Offline

Gender: Male
Posts: 1114

276733708 276733708 clanguardian2003 Ol+Grimmy
View Profile WWW E-Mail
Re:*nix command line args
« Reply #3 on: March 20, 2004, 04:34:40 PM »
Reply with quote

no, no, a C-shell script....not a c++ pgm....

Code:

if (#$argv == 0) then
  # run code here to prompt user for seconds.
else if (#$argv == 1) then
  # this implies $1 == input value in seconds.
    # then grounded is kinda right...but I have to use a combination of integer division, and modulo to convert...
  @seconds = $1 / 60
  @temp = $1 % 60
  # etc, etc, etc....


the problem is I need conditions to make sure that $1 is indeed an integer value, and not some random garbage...

I could call the function:

% timer 9032

or:
% timer five

technically five is still one arg, and therefore, would work with the code I have, but unix unlike c++ will not turn 'five' into a numerical value based on ascii characters...so I need to damn near grep through it looking  for '^[0-9]+'

but how i can imbed that in my code beats the hell out of me...

Tenshi
 
Logged

Terraji
Admin Team
CSR Connoisseur
*****
Karma: +35/-15

Offline

Gender: Male
Posts: 789

terraji@hotmail.com
View Profile E-Mail
Re:*nix command line args
« Reply #4 on: March 20, 2004, 05:30:53 PM »
Reply with quote

Okay, then it is out of my area of expertise
I'm gonna have a peek in a textbook that I own that I think relates. At the very least, it might help me learn something about c shell scripts.
Logged
Porter
[Wumpa]
Board Admin
*****
Karma: +176/--88

Offline

Gender: Male
Posts: 3910

Wumpa+Porter
View Profile WWW E-Mail
Re:*nix command line args
« Reply #5 on: March 22, 2004, 01:51:13 PM »
Reply with quote

I hate it when you're forced to use tools that aren't designed to do what you've been asked to do. Much less c-shell! VERY unfriendly. Shell scripts should pretty much always be bash based. Plus, perl could do this entire assignment in ~4 lines of code.

What really gets me is that poor Tenshi is being TAUGHT bad practices.... instead of picking them up on his own like any programmer worth his salt.

Sorry tenshi, I don't know shell scripting well enough to be able to help. And of course, since I've been away for a while, I'm probably too late to help anyway.
Logged

[Wumpa] Porter
  --Silent, professional, lethal... sometimes.
slightcrazed
-TWB-
Admin Team
CSR Connoisseur
*****
Karma: +65/-7

Offline

Gender: Male
Posts: 983


View Profile
Re:*nix command line args
« Reply #6 on: March 22, 2004, 03:01:46 PM »
Reply with quote

Quote from: Porter on March 22, 2004, 01:51:13 PM
Shell scripts should pretty much always be bash based.

Agreed. c-shell is quickly becoming outdated, if it isn't already there.

Quote:
Plus, perl could do this entire assignment in ~4 lines of code.


So could python and half a dozen other languages. I could probably bang out a bash script that was about 10 lines, which isn't to shabby for a command line language.

Quote:
What really gets me is that poor Tenshi is being TAUGHT bad practices.... instead of picking them up on his own like any programmer worth his salt.
So true. Programming is a language in and of itself, and there is a right way, and a wrong way to do thing. "Me go store" might get the point across, but "I am going to go to the store" is much more properer. (play on words )

I'm not sure if bash includes the ability to test for input type (I believe it does), but you might be able to fudge it by testing the value against a multiplier. If 'var1' is your input variable, then multiply it by 1, and if it is text you will get a 'null' response. If it is numerical then you can grep it for a '.' and if one is found, then you can yell at the user to use whole numbers only.
It's ugly, but it would probably work. I'm going from memory, I'd have to check the bash scripting guide to be more help.

Which reminds me, do a google for 'bash scripting guide' and you should get about 50,000 hits for it. Probably the best reference there is out there.

Then you just need to spend a couple days convincing your teacher to let you use bash, instead of c-shell.

She sells c-shells by the sea shore.

slight
Logged

I once beat Drizzt Do'Urden at thumb wrestling.
Guardian_Tenshi
Global Moderator
*****
Karma: +53/-26

Offline

Gender: Male
Posts: 1114

276733708 276733708 clanguardian2003 Ol+Grimmy
View Profile WWW E-Mail
Re:*nix command line args
« Reply #7 on: March 22, 2004, 07:17:08 PM »
Reply with quote

Well, alot of help you guys are    but thankfully slight was right about looking on the internet, i was reading some stuff, about egrep and figured it out.  I'm not sure this is the BEST way, but this way works...

Code:

...
...
echo "$1" | egrep '^[0-9]+$' >& /dev/null
if ($status == 0) then
...
...
...
...


Thanks for the help though guys.  Surprisingly, this time I think I stumped Porter.  yay!! for me...

Tenshi
Logged

Guardian_Tenshi
Global Moderator
*****
Karma: +53/-26

Offline

Gender: Male
Posts: 1114

276733708 276733708 clanguardian2003 Ol+Grimmy
View Profile WWW E-Mail
Re:*nix command line args
« Reply #8 on: March 23, 2004, 11:56:47 PM »
Reply with quote

Code:

#!/bin/csh
# this program will takes one integer argument in seconds
# it will convert the num of seconds to secs, mins, hrs, days.
# This program is written by John A. Lillard

# Number of Argument Validation
if ($#argv == 0) then
  echo -n "Please enter an integer value in seconds: "
  set initval = $<
  # initval should be an int, but will be checked later.
else if ($#argv > 1) then
  echo "Invalid number of arguments for $0"
  echo "Usage: $0 [int seconds]"
  exit 1
else
  set initval = $1
  # initval should be an int, but will be checked later.
endif

# Is the argument an integer value??
echo "$initval" | egrep '^[0-9]+$' >& /dev/null
if ($status == 0) then
# math to do conversion below...
  @ days = $initval / 86400
  @ temp1 = $initval % 86400
  @ hours = $temp1 / 3600
  @ temp2 = $temp1 % 3600
  @ minutes = $temp2 / 60
  @ seconds = $temp2 % 60
else
  echo "Invalid argument given"
  echo "Usage: $0 [int seconds]"
  exit 1
endif

# Begin Printing Data...
if ($seconds == 1) then
  echo "1 second"
else if ($seconds != 0) then
  echo "$seconds seconds"
endif

if ($minutes == 1) then
  echo "1 minute"
else if ($minutes != 0) then
  echo "$minutes minutes"
endif

if ($hours == 1) then
  echo "1 hour"
else if ($hours != 0) then
  echo "$hours hours"
endif

if ($days == 1) then
  echo "1 day"
else if ($days != 0) then
  echo "$days days"
endif

exit 0
Logged

slightcrazed
-TWB-
Admin Team
CSR Connoisseur
*****
Karma: +65/-7

Offline

Gender: Male
Posts: 983


View Profile
Re:*nix command line args
« Reply #9 on: March 24, 2004, 07:29:06 AM »
Reply with quote

Looks good, although I think you might have been able to work in some simpler code using a case statement, as opposed to all the if statements at the end. Looks good none-the-less.

slight
Logged

I once beat Drizzt Do'Urden at thumb wrestling.
Pages: [1] Reply Notify of replies 
CSReloaded Forums  |  General Category  |  Help & Troubleshooting (Moderator: Guardian_Tenshi)  |  Topic: *nix command line args
Jump to: 

Powered by PHP CSReloaded Forums | Powered by YaBB SE
© 2001-2003, YaBB SE Dev Team. All Rights Reserved.
Powered by MySQL
:[ Site Design by Ryo, scripts and backends by Porter and Ryo, banner by Supafly! Powered by PHP and MySQL ]:
Page created in 0.057 seconds.