how to check first character in var's value.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to check first character in var's value.
# 1  
Old 07-01-2005
how to check first character in var's value.

Currently my script works like this:

Code:
DATE_dd=5 # Assume this is user input. (User can enter 1, 2, 3, .. 10, 11, 12)

if [ ${#DATE_dd} == 1 ]; then
        DATE_D=" ${DATE_dd}"
        file_name="${DATE_yyyy}${DATE_mm}0${DATE_dd}.csv"
    else
        DATE_D=${DATE_dd}
        file_name="${DATE_yyyy}${DATE_mm}${DATE_dd}.csv"
    fi

The way it is now is, it checks the length of the variable, but I want to change it so that it will check to see if the first character of the value of the variable is zero or not so that the user can input values like "06, 07, 08" instead of "6, 7, 8".

I am aware of ${variable:start}, ${variable:start:length}, but could i stick that in the if statement?

if [ ${DATE_dd}, ${DATE_dd:0:1} == 1 ]; ?

Because if I want to run DATE_dd=`date +%d` then on a day like today "01" it will fail to GREP the LS -AL command to today's date, since the ls -al displays the day as a single digit, without padded zeroes.

Last edited by yongho; 07-01-2005 at 11:46 AM..
# 2  
Old 07-01-2005
Quote:
Originally Posted by yongho
I am aware of ${variable:start}, ${variable:start:length}, but could i stick that in the if statement?
What shell is that?

in ksh you can do it like that:
Code:
#!/bin/ksh
typeset -Z2 number

number=7
echo $number

number=23
echo $number

# 3  
Old 07-01-2005
hm

That's actually in Korn Shell. I found it here on google.

http://www.awprofessional.com/articl...99035&seqNum=3

It's one of the last examples way at the bottom.
# 4  
Old 07-01-2005
typset -Z2

The Typeset -Z2 command you told me about works really great for the if statement I posted before, since now I don't even need the if statement. But later on in the script, I need to take the value of the day in that variable and run a GREP for "today's" files.

For example:

ls -al <path> | grep "$date_b $date_d"

This will retrieve all files/directories that were created "today" in this format:
"Jun 22" or "Jun 24"... However this command breaks for a day like today "Jun 01". It fails because ls -al returns a day value that looks like this "Jun 1" (double space between Jun and 1). Instead of a padded zero, it's just replaced with a space.

So my script failed to work today because it tried to grep "Jun 1" instead of "Jun 1" (double space between Jun and 1). Is there a way I can check to see if the `date +d` command gave me a padded zero or not? (Check the first character of the string to see if it's zero, and if so, replace with a space).

Code:
#!/bin/ksh

date_b=`date +%b`
date_d=`date +%d`

ls -al <path> | grep "$date_b $date_d"

The script above only works for the days of the month that are between the 10th and the 31st, but fails for all values betwee 01 and 09.
# 5  
Old 07-01-2005
Quote:
Originally Posted by yongho
That's actually in Korn Shell. I found it here on google.

http://www.awprofessional.com/articl...99035&seqNum=3

It's one of the last examples way at the bottom.
they don't mention it, but it only applies to the newer shell - ksh93
for example Sun's stock ksh is ksh88 without that capability.

on the other hand Sun's /usr/dt/bin/dtksh is ksh93-compatible.

Last edited by vgersh99; 07-01-2005 at 12:24 PM..
# 6  
Old 07-01-2005
echo 'June 01' | sed 's/ 0/ /'
# 7  
Old 07-01-2005
thanks

thanks..!

I spent way too much time using AWK and haven't touched SED enough yet.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Var Check Script (Help improve if possible)

I am working on a script to check the var on all of my systems. Can someone help me fix it to work better or give me suggestions. #!/bin/ksh IN=/path/to/list_of_workstations.txt while read hostnames do if ping $hostnames 1 | grep alive > /dev/null then percent=`ssh -q... (3 Replies)
Discussion started by: whotippedmycow
3 Replies

2. Shell Programming and Scripting

Help with a shell script to check /var file system

deleting (0 Replies)
Discussion started by: fretagi
0 Replies

3. UNIX for Dummies Questions & Answers

Check to see if string var is empty

i have a veriable set var1 set var2 = abcd how can i check if var 1 is empty and if var 2 is not empty ??? (2 Replies)
Discussion started by: nirnir26
2 Replies

4. Shell Programming and Scripting

How to check for special character in a value

Hi, I have a variable and to it always alphanumeric value will be assigned. If the value has any special characters in it then in the if statement it should exit like below if (value has any speacial character) then exit else .... fi can any one suggest how to acheive this? (4 Replies)
Discussion started by: lavnayas
4 Replies

5. Shell Programming and Scripting

check number of character

hi, I would like to calculate number of character for a number, for exemple : 1200 --> there are 4 characters , 120001 -> 5 characters (4 Replies)
Discussion started by: francis_tom
4 Replies

6. Shell Programming and Scripting

check whether it is a non-numeric character

Below is the abstract of the script which is working fine. if ] then error_process "Invalid month format." return 1 fi I am doing validation for month and it errors if the value is > 12 or < 0. In addition, I want to add another condition to error if it... (2 Replies)
Discussion started by: sony_dada
2 Replies

7. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies

8. Shell Programming and Scripting

how to check string of character

how can i check whether variable contains only character from a-z or A-Z....if my variable contains any alpha numeric, numeric or any character with some special one i.e. *%&@! etcetera etcetera....then it should show me please enter only characters...... Let my variable var1="abc77}|" then... (9 Replies)
Discussion started by: manas_ranjan
9 Replies

9. Shell Programming and Scripting

Bourne: search for a non-numeric character in $VAR

if $1 = "123x456", how can I test for the non-numeric character 'x' in that string. I've tried expr with "" but it did not find the x. Any ideas? Can this perhaps be done with sed? Thanks. (2 Replies)
Discussion started by: lumix
2 Replies

10. UNIX for Dummies Questions & Answers

check for the first character to be blank

I'm having a problem when the first line or first character of a file is blank. I need to get rid of both of them when they occur but don't want to delete the line. Does anyone have any suggestions? (7 Replies)
Discussion started by: anthreedhr
7 Replies
Login or Register to Ask a Question