Extracting first character from a user response


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting first character from a user response
# 1  
Old 10-18-2009
Extracting first character from a user response

I am using the following code in a CShell script to get a yes/no response from the user:

echo ""
echo -n "Do you want to archive your main level directory? <y> or n: "
set main_answer = $<


Is there a way to extract the first letter from the user's response and then perhaps convert that letter to lower case?

Thanks,
Paul Hudgens
Denver
# 2  
Old 10-18-2009
As I've yet to find a useful case for the C-shells existence...

Code:
#!/bin/csh
set main_answer = $<

echo $main_answer | cut -c1 | tr '[a-z]' '[A-Z]'

# 3  
Old 10-19-2009
Extracting first character from a user response

Scott,
The:
Code:
echo $main_answer | cut -c1 | tr '[a-z]' '[A-Z]'

code works on both Unix and Linux, but I'm having difficulty capturing that first character of the user response in a variable that can be checked later. I've tried serveral different combinations, but when I echo $main_answer to the screen, I always get the full response entered at the prompt. Thanks for any help,
Paul Hudgens
# 4  
Old 10-19-2009
Code:
#!/bin/csh
set main_answer = $<

set short_answer=`echo $main_answer | cut -c1 | tr '[a-z]' '[A-Z]'`
echo $short_answer

# 5  
Old 10-19-2009
Extracting first character from a user response

This works - thanks for the help.

I had tried exactly what you show, only I re-used main_answer instead of a new variable (in this case short_answer). Apparently that doesn't work in CShell like it does in other languages.

Thanks,
Paul Hudgens
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Unable to create new user without lower character

Hi friends, I want to create new user BG0001 in SunOS and getting below errorbash-3.2# useradd -d /home/BG0001 -m -s /bin/sh BG0001 UX: useradd: BG0001 name should have at least one lower case character. bash-3.2# bash-3.2# OS version is as belowbash-3.2# cat /etc/release ... (7 Replies)
Discussion started by: sandeepkmehra
7 Replies

2. Shell Programming and Scripting

Bash to goto specific line/function and start processing if user response is yes

In the bash below I am trying to run the script entire script including the ....(which is a bunch of code) and then in the run function if the user response is y (line in bold). then start processing from execute function. Basically, goto the # extract folder for variable filename line and start... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Extracting character between 2 token - using only first match

Hello. ps -ae return I would like that the following command return 3214 echo " 3214 ? 00:00:01 acroread" | grep -o "]]]*" (5 Replies)
Discussion started by: jcdole
5 Replies

4. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

5. UNIX for Dummies Questions & Answers

extracting character

This time I am trying to extract the number 10 from the following line. This number is subject to change and may be anything from 1 to 3 numerals in length, i.e. 1-999. Hence why I dont want to use 'cut' cmd. The line I am working on is GGSN-MIB::ggsnApnName.10 = STRING: open.internetI have... (7 Replies)
Discussion started by: rob171171
7 Replies

6. Shell Programming and Scripting

Extracting the last character of a string

How to extract the last character of a string in bash? ---------- Post updated at 03:56 PM ---------- Previous update was at 03:55 PM ---------- Suppose "abcde" is a string. i want to extract the last character i.e. "e". (1 Reply)
Discussion started by: proactiveaditya
1 Replies

7. Shell Programming and Scripting

Taking user response in perl

I was wondering if it was possible to take in a user's response in a perl program. For example. Ask user a yes or no question, and then depending on the response, print out a corresponding answer? Thanks! (2 Replies)
Discussion started by: silkiechicken
2 Replies

8. Shell Programming and Scripting

Extracting a substring starting from last occurance of a string/character

Hi All, This is Ram. I'm new to this forum & new to shell scripts as well. I've a requirement in which I want to extract a substring from a given string based on last occurance of a character. for eg. I have a string of a file name with absolute path like... (2 Replies)
Discussion started by: krramkumar
2 Replies

9. Shell Programming and Scripting

read-waiting for user response

Would there be any reason for why a 'read ans' would not wait for a user's response (i.e user has to hit a key to continue)? I know for sure that it is doing everything else in that part of my 'if' statement but it doesn't wait for me to hit a key before continuing. The strange thing is that... (4 Replies)
Discussion started by: giannicello
4 Replies
Login or Register to Ask a Question