Sponsored Content
Top Forums Shell Programming and Scripting Ensuring A String Doesn't Begin With Space Post 302231771 by dennis.jacob on Wednesday 3rd of September 2008 05:57:17 AM
Old 09-03-2008
Quote:
Originally Posted by Silentwolf
In my continuing struggle with my menu for record manipulation, I've been scripting to add records to the 'records' file. For the most part, I've been successful. I am, however, running into a strange problem.

Code:
                                while [ ! $gn_correct ]
                                do
                                        echo -n "Given name: "
                                        read g_name
                                        if [ ! $g_name ] ; then
                                                echo "Given name not entered."
                                        elif [ `expr substr $g_name 1 1` = " " ] ; then
                                                echo "Given name may not begin with a space."
                                        elif [[ $g_name = $(echo "$g_name" | tr -dc '[A-za-z ]') ]] ; then
                                                gn_correct=1
                                                echo -n "$g_name:" >> records
                                        else echo "Given name can contain only alphabetic characters and spaces"
                                        fi

                                # Close given name loop.
                                done

The above code reads in a person's given name. It makes sure that a name has been entered, then proceeds. The problem lies here:

Code:
elif [ `expr substr $g_name 1 1` = " " ] ; then
                                                echo "Given name may not begin with a space."

The idea is to test to make sure the first character in the string received for the given name is not a space and return an error-echo if it is. It'll then repeat the loop, so the user is given another opportunity to get it right. In theory this continues until a valid given name is received.

However, it doesn't work this way. Instead, this code is skipped over. Spaces are truncated from entries with a leading space anyway - but the problem is, I'm using this same code to collect job title information. Job titles often have spaces in them (though not at the start.)

The expr continues to fail at picking up leading spaces - but, more strangely, if there is a space somewhere else in the string, expr encounters a syntax error, and the echo displays that a job title may not begin with a space.

Any suggestions on how I could detect leading spaces and produce an error, while still allowing spaces elsewhere in the string?

Thanks in advance for help,
Wolfie
You can try something like :
Code:
echo "$var" | grep -q '^ '


Output:
Quote:
/home/ans >x=' zxc'
/home/ans >echo "$x"
zxc
/home/ans >echo "$x" | grep '^ '
zxc
/home/ans >x="fds"
/home/ans >echo "$x" | grep '^ '
/home/ans >
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk BEGIN END and string matching problem

Hi, Contents of BBS-list file: foo foo foo awk ' BEGIN { print "Analysis of \"foo\"" } /foo/ { ++n } END { print "\"foo\" appears", n, "times." }' BBS-list Output: Analysis of "foo" "foo" appears 3 times. awk ' (3 Replies)
Discussion started by: cola
3 Replies

2. Shell Programming and Scripting

Replace string with sed doesn't work

Hello, Unfortunately I don't found any working solution for my problem :/ I have pass file for dovecot authorizing in this format: user@domain.tld:{SSHA}Ykx2KVG/a2FKzjnctFFC2qFnrk9nvRmW:5000:5000:::: . . ...Now, I want to write some sh script for password changing for grep'ed user, in... (5 Replies)
Discussion started by: vincenty
5 Replies

3. Shell Programming and Scripting

Check whether a string begin with uppercase, lowercase or digit!

Hi every body! I wrote script on Fedora (bash shell) to check whether a tring enter from user console is start with a uppercase/lowercase letter or a digit. But with this script i have some problem when I enter from character from 'b' to 'z' --> result is uppercase. This code look like ok but i... (9 Replies)
Discussion started by: nguyendu0102
9 Replies

4. UNIX for Dummies Questions & Answers

Ensuring / at end of directory path

I have a variable name storing a directory path set dirpath = /home/chrisd/tatsh/branches/terr0.50/darwin I want to ensure there is always a "/" at the end, and if there are more than one "/", that I reduce it to just one. ---------- Post updated at 08:16 AM ---------- Previous update was... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Inserting new line if two sequential lines begin with the same string

Hi I have a file like this: Peter North Mary Peter North Peter Borough Mary I need there to put 'X' (or anything) on a new line between the two lines where 'Peter' begins the line. There will be many matches to this string, but they will always begin with 'Peter'. Ie, the resulting... (2 Replies)
Discussion started by: majormajormajor
2 Replies

6. Shell Programming and Scripting

How to test that a string doesn't contain specific characters?

Hi ! :) I made this : #!/bin/bash rsa_dir="/etc/openvpn/easy-rsa/" rsa_key_dir="/etc/openvpn/easy-rsa/keys/" ccd_dir="/etc/openvpn/ccd/" regex_special_char='' cd $rsa_dir while read -p "Please can you enter the vpn's username : " username ] || ] || ] || ] do echo "Your entry... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

7. Shell Programming and Scripting

Ensuring certain processes do not show up in process table

i've read somewhere a long time ago that there's a way to hide the workings of a script in sub functions so that when run, it doesn't show up in the process table. is this really possible? for instance, i need to run a command that has a password in it. and when that;s being run, it can be... (2 Replies)
Discussion started by: SkySmart
2 Replies

8. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies

9. UNIX for Beginners Questions & Answers

C shell concatenate string doesn't work

I have the following code: #!/bin/csh clear set cloud_file="/home/labs/koren/davidsr/general_scripts/MFP_10_PP_Parmas.txt" # to fill set mie_tables_dir='/home/labs/koren/davidsr/SHDOM_MAIN/MIE_TABLES/non_polo_wave_0.7_water_50R_s0.5_e25_max_70.mie' # to fill set prp_dir='${pp_dir}/pp_prp/'... (2 Replies)
Discussion started by: student_wiz
2 Replies
All times are GMT -4. The time now is 04:12 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy