Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook 2007 outlook express partition password problem router slow software sound trojan usb video virus vista windows windows xp wireless
UNIX/Linux
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > UNIX/Linux >
Solved: ksh question


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
surfnschultz's Avatar
Senior Member with 191 posts.
 
Join Date: Nov 2003
03-Jun-2005, 02:01 PM #1
Smile Solved: ksh question
How would I capture to compare the first number in a 3 or 4 digit string

I have set in the .profile a user stornum and they enter a port number
and I want to make sure the first digit of the 3 or 4 digit long numeric is equal to the stornum value

STORNUM="4"
portnum = 4321

I want to do this

if ($STRONUM -eq (1st digit of portnum))then
continue
else
exit
fi

thanks for any tips the race to solution is on.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
03-Jun-2005, 11:47 PM #2
You don't mention what shell you're using, but I'll assume it's a Bourne-shell type (i.e., sh, bash, ksh) and not a C-shell type (csh, tcsh). If you know your portnumber will always be 4 digits, you can obtain the first digit by dividing by 1000; try:

portnum=7381
digit=`expr $portnum / 1000`
echo first digit is $digit

Also, you may be able to use an index/substring function (available in ksh and bash, I believe) to extract the first character of a string, which has the advantage of not needing to know the number of digits in the portnumber value. You can also determine the correct divisor to use by checking the length of the portnum value. You could code a more general solution something like this:
Code:
portnum=829
divisor=1000       # must be at least 10^((maximum length of portnum) - 1)
digit=0

while [ $digit -eq 0 ]
do
    digit=`expr $portnum / $divisor`

    if [ $digit -eq 0 ]
    then
        divisor=`expr $divisor / 10`
    fi
done
echo first digit is $digit

Hope this helps.
__________________
The slowest component still sits at the keyboard.
Squashman's Avatar
Distinguished Member with 12,689 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
05-Jun-2005, 08:40 PM #3
firstdigit=`echo $portnum | cut -b1`
or
firstdigit=`echo $portnum | cut -c1`
surfnschultz's Avatar
Senior Member with 191 posts.
 
Join Date: Nov 2003
06-Jun-2005, 01:36 PM #4
This is all in KSH, on a AIX system.

I am setting a value STORNUM in the .profile to be the store number the employee comes from. The first digit of the portnumber would be the store number.

at the prompt echo $STORNUM I get a number like 4

in my script if I put echo $STORNUM I get nothing.

I tried
echo `$STORNUM`
echo !$STORNUM
echo `STORNUM`

How do I echo the .profile value to the screen?

I also had a problem I was confused about If I write in my script
firstdig = `ceho $portnum | cut -b1`

I get an error saying cannot find firstdig???? How do create a variable to assign a value to in KSH?

anyway monday morning confusion.

Goal is to read STORNUM and compare to first digit then decide if number are equal to proceed if not exit

if [ `echo $portnum | cut -b1` -eq $STORNUM ] then
else
exit
fi

can't get STORNUM part to work?

Thanks for any help
Squashman's Avatar
Distinguished Member with 12,689 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
06-Jun-2005, 10:25 PM #5
Quote:
Originally Posted by surfnschultz
I also had a problem I was confused about If I write in my script
firstdig = `ceho $portnum | cut -b1`

I get an error saying cannot find firstdig???? How do create a variable to assign a value to in KSH?
Don't use spaces
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
06-Jun-2005, 11:28 PM #6
Quote:
in my script if I put echo $STORNUM I get nothing.

I tried
echo `$STORNUM`
echo !$STORNUM
echo `STORNUM`

How do I echo the .profile value to the screen?
There are several things going on here:
(0) as the Squashman notes, there should be no spaces on either side of the equals sign in an assignment statement.

(1) different quotes mean different things to the shell. For example, single quotes mean (roughly) "take everything I typed literally" -- i.e., if you type asterisk (*), then the shell does NOT expand it; rm * is NOT the same as 'rm *'. The first attempts to remove all files in the current directory; the second attempts to remove a file named * in the current directory. Double quotes allow filename and globbing (wildcard) expansion, as well as environmental variable substitution. For example, rm "this is a test" attempts to remove a file named "this is a test" (without the quotes) in the current directory. The command: rm this is a test attempts to remove four files named this, is, a and test in the current directory. In this case, there would be no difference between the single and double-quote meaning, because the command contains no globbing, filename expansion or variable substitution. However, the command rm 'this is*' is not the same command as rm "this is*"; the first attempts to remove a file named "this is*" in the current directory, while the second attempts to remove all files that begin with the characters "this is" in the current directory.

(2) back-tics (accent grave -- i.e., `) execute what is contained between the back-tics as a command.

(3) the exclamation point is usually used as a shell escape from within a program (such as vi, for example) and often allows you to execute a command and capture the output. Note that you may need a space between the exclamation point and the command to be executed. However, if you code echo !$STORNUM, the shell will first perform variable substitution and replace $STORNUM with its value (4, in this case?) and then display !4 to the screen.

Here's how I suspect the shell interpreted your attempts:
Code:
echo `$STORNUM`      -> displayed $STORNUM (literally) on the screen
echo !$STORNUM        -> displayed ! followed by the value of STORNUM on the
                                         screen
echo `STORNUM`        -> shell attempts to execute a command named STORNUM 
                                         and echo the results.  Since there is likely no command 
                                         named STORNUM, the shell produces an error.
Quoting can be very confusing (and that's only the beginning! ). Hope this helps.
__________________
The slowest component still sits at the keyboard.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:10 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.