Computing.Net > Forums > Unix > Shell Script Help Needed

Shell Script Help Needed

Reply to Message Icon

Original Message
Name: Beaker
Date: January 29, 2002 at 11:33:22 Pacific
Subject: Shell Script Help Needed
Comment:

Hi~ I'm running NetBSD-1.5.2 on the i386 platform w/ wscons enabled (virtual screens 0 thru 3) and am trying to add some C-shell scripting to my ~/.cshrc file that does the following:

1. executes 'tty' and strips the last character off the output and assign it to a variable x (ie. x=3 if tty rtns "/dev/ttyE3")

2. set prompt = "^[[3x;40m$USER@$HOST:"
where x is variable from step 1.
* note: ^[ is the literal ESC character used to set the ansii foreground/background color indicated by [3(fg);4(bg)m

What I've tried -unsuccessfully - is listed below (executed as standalone script for testing purposes):
---
#~/colortest
#!/bin/csh

# script reads tty and sets ascii text color
# "[31;40m = red ; [32;40m = grn ; [33;40m = blue

set scrn = (`/usr/bin/tty | /usr/bin/tail -c 2`)
if ( $scrn == 0 || scrn >= 3 ) then
exit
else
echo ^[[3$scrn;40m
endif

#end of script
---
I'm sure its some silly little thing but being a novice I'm completely stumped.
Beaker


Report Offensive Message For Removal

Response Number 1
Name: James Boothe
Date: January 29, 2002 at 16:17:59 Pacific
Subject: Shell Script Help Needed
Reply: (edit)

1. In your if-statement, 2nd reference to scrn is missing the dollar-sign.

2. If any of your tty values end in a non-digit, the if-statement, expecting a numeric test, will display "Expression syntax".

3. Your echo includes a semicolon, which is used for command separation. So, echo abc;def would echo abc and then try to locate and run def. But echo abc\;def would echo the 7-character string.

4. If you want a variable concatenated with other letters/digits, then isolate with braces. If I want to echo $myvar followed by x, $myvarx would not get it, so I would have to echo ${myvar}x.


Report Offensive Follow Up For Removal

Response Number 2
Name: Beaker
Date: January 29, 2002 at 22:23:43 Pacific
Subject: Shell Script Help Needed
Reply: (edit)

Thanks James for your input: I've got the test script more or less working. One thing I'm stuck on is how to weed out non-integer values passed to $scrn (see below):
---
#!/bin/csh
start:
echo "Enter an integer between 0-3:"
set scrn = $<
if ( $scrn !~ [0-3] ) then
echo "{$scrn} is not a valid entry" ; goto start
else
echo "Good - {$scrn} is valid integer"
endif
---
Entering "a" or something other that a number results in a "if: Expression Syntax" error. Is there some of stipulating something like

IF X EQUALS ANYTHING BUT (0-3) THEN EXIT

where ANYTHING includes strings & mixed numbers?

Thanks Again,
Beaker


Report Offensive Follow Up For Removal

Response Number 3
Name: James Boothe
Date: January 30, 2002 at 07:11:12 Pacific
Subject: Shell Script Help Needed
Reply: (edit)

I think the check you are doing would be the most appropriate way to check for a single digit 0-3. The =~ and !~ are definitely string operators. Furthermore, I copied and pasted your code verbatum, and it ran with no problems, properly rejecting all entries except a single digit 0-3.

I don't use csh, but that's pretty straight-forward code, and I'm surprised you have a problem with it. Why do you use csh? It is widely recognized as the worst shell by far, fraught with bugs and things it just cannot do.


Report Offensive Follow Up For Removal

Response Number 4
Name: dudi
Date: May 12, 2002 at 04:31:43 Pacific
Subject: Shell Script Help Needed
Reply: (edit)

hi
i want to write ascript in csh , that
kiled all user that idle more than 30 minutes.
please help me!!!!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Shell Script Help Needed

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software