Accepting A-Za-Z


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accepting A-Za-Z
# 1  
Old 10-14-2010
Accepting A-Za-Z

Is there a way accept A-Za-z0-9 from the user from a parameter?

EX. I want to take the parameter from the user even if its hEu or H3y
and store it as a parameter ( $1 )
# 2  
Old 10-14-2010
$( echo 'Enter Parameter: \c' >/dev/tty ; line )
# 3  
Old 10-14-2010
Code:
# ./justdoit hEu
'$1'= hEu

Code:
# ./justdoit H3y
'$1'= H3y

Code:
# ./justdoit H3E
'$1'=

Code:
## justdoit ##
 
#!/bin/bash
parameterone="$1"
if [[ "$parameterone" == "hEu" ]] || [[ "$parameterone" == "H3y" ]] ; then
            set -- "$parameterone" $1
else
            set -- "" $1
fi
echo "'\$1'= $1"

# 4  
Old 10-14-2010
Is there a way to convert everything to caps after you take it in?
# 5  
Old 10-14-2010
If you want to restrict the input, this might work, except sed will want an extra linefeed:

Code:
$(
 echo 'Enter Parameter: \c' >/dev/tty
 sed '
   /[^a-zA-Z0-9]/{
      s/.*/Must contain only letters and numbers: "&"/
      w /dev/tty
      d
     }
   q
  '
 )


This is a bit busier, but you see you can do almost anything in the $() to get the parameter. narrative: Capture the stdout of a subshell as a single parameter, set output_var blank, while output_var is blank, prompt for the parameter and read one line, saving it for error output. If the line is blank or has a forbidden character, it is rejected by displaying it including expansion of any control characters and looping back else echo it out of the subshell as the parameter:

Code:
$(
   l2=""
   while [ "$l2" = "" ]
   do
    echo 'Enter Parameter: \c' >/dev/tty
    line | read l
    echo "$l" | sed '/[^a-zA-Z0-9]/d' | read l2
    if [ "$l2" = "" ]
    then
     echo "Invalid, must be only numbers and letters and not empty: '$l'" | cat -vt >/dev/tty
    fi
   done

  echo "$l2"
 )

Add " | tr '[a-z] '[A-Z]' " or
" | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' " for all caps -- there may be other more specific options and commands, but this general solution suffices and fits other needs, too. Since there is sed in the scripts above, it can be done at the end of the current sed script parameter.

Last edited by DGPickett; 10-15-2010 at 11:13 AM.. Reason: wrong sed capitalize command
# 6  
Old 10-14-2010
Quote:
Originally Posted by puttster
Is there a way to convert everything to caps after you take it in?
Code:
#!/bin/bash
parameterone="$1"
if [[ "$parameterone" == "hEu" ]] || [[ "$parameterone" == "H3y" ]] ; then
            caps=$(echo "$parameterone" | perl -pe 'y/a-z/A-Z/' )
            set -- "$caps" $1
else
            set -- "" $1
fi
echo "'\$1'= $1"

# 7  
Old 10-14-2010
say I wanted to add ABC at the end of every blank space, why does this not work?

Nevermind I figured out a different way of doing it :P thanks anyways on the adding characters after a blank space.

Last edited by puttster; 10-14-2010 at 06:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parameter not accepting in awk

In below script parameterwhich i am passing to awk function is not working.Not sure why its not accepting parameter.Please tell me how to pass parameter to the awk function.Please check the below script. #!/bin/ksh dummy_file=/etlapps/dev/data/sub_servicer_data/ScriptLogs/emp.txt fields=5... (2 Replies)
Discussion started by: katakamvivek
2 Replies

2. Shell Programming and Scripting

script accepting password

Hi friends, I am very new to Unix scripting and having some difficulty in my first shell script. I have written a simple shell script to upload an artifact to a remote machine on the network. echo "Uploading the artifact" scp app.war username@remotemochine.domainname.net:/home/deployables... (3 Replies)
Discussion started by: prashdeep
3 Replies

3. Shell Programming and Scripting

accepting passwords in shell programming

how do i accept a password like input through keyboard using shell script? i dont know the command for doing so. (5 Replies)
Discussion started by: kz2013
5 Replies

4. Shell Programming and Scripting

Accepting Input regardless of Case

Hi I am trying to get my script to accept input regardless if the person enters a or A. here is the portion of the code where I get the input. echo -n 'Please enter your choice:' # prompt user for input. read reply # read input echo case $reply in #... (2 Replies)
Discussion started by: DualPandas
2 Replies

5. Shell Programming and Scripting

Help with Accepting Directories as an Argument

I'm doing a script where you are suppose to start off in accepting one or more directory as an argument. How do i do this? Sorry for the nub question. (2 Replies)
Discussion started by: LinuxUser232331
2 Replies

6. Shell Programming and Scripting

reg accepting password

Hi, I want to login to many systems and password should be taken automatically from a file(login is working but password is not accepting). Any help on this is appreciable . My code. for i in `cat /tmp/tes ` ====>tes file contain list of hosts > do ssh $i > perl prog.pl >... (1 Reply)
Discussion started by: rogerben
1 Replies

7. UNIX for Dummies Questions & Answers

accepting input date

I how do i accept a input date in script which is lesser than a specified day? ex: to accept a date less than or equal to 100 days(from today).?:( Thanks for the help in advance.:) (1 Reply)
Discussion started by: abhi_123
1 Replies

8. Shell Programming and Scripting

Not able to display the value I am accepting as an argument

Hi, I am new to UNIX. I am facing some problem here. #! /usr/bin/ksh currDate = $1 export currDate; echo " Date is $currDate" when I run this script, it says : currDate not found. Can anybody point out the mistake please. --mahek (3 Replies)
Discussion started by: mahek_bedi
3 Replies

9. HP-UX

Oracle not accepting new connections

Hi UNIX guru's, Have recently upgraded Oracle from 8i to 10g on an HP-UX (RISC) 11.11 box. At least twice a day the database stops accepting incoming connections and the following errors are observed in the various logs. The box needs to be rebooted to get everything going again. The... (4 Replies)
Discussion started by: mat_cottrell
4 Replies

10. Shell Programming and Scripting

Accepting User Input

I'm just starting out with UNIX and have figured some stuff out. I just need some help with accepting user input on the command line. For instance, I created a number counter that counts down from any positive hard coded number. But, I want the commnad line line to read "Countdown 20" where 20... (1 Reply)
Discussion started by: scott78
1 Replies
Login or Register to Ask a Question