LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-05-2013, 10:04 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Why use ${REPLY:0:1} and not $REPLY


What are the semicolon values for - is it to shift values?

Code:
echo -e "Do you wish to restore all these file(s)/directory(ies)? [Enter Y to restore all; N to choose single file]: \c"
        read
        if [[ ${REPLY:0:1} = y ]] || [[ ${REPLY:0:1} = Y ]]; then
 
Old 03-05-2013, 10:08 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
To me, it's look like 'getopts' command line aguments. Can you post full script?
 
Old 03-05-2013, 10:23 AM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,795

Rep: Reputation: 990Reputation: 990Reputation: 990Reputation: 990Reputation: 990Reputation: 990Reputation: 990Reputation: 990
That's how you extract a substring from a string in BASH

http://tldp.org/LDP/abs/html/refcards.html#AEN22664

All they're doing is extracting the first character from whatever the user entered and checking if it's a y or Y.
 
1 members found this post helpful.
Old 03-05-2013, 10:28 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 11,890

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
${REPLY:0:1}
It is a substring function of the form ${string:position:length}. The first character position of a string is 0. Just some error checking.
 
Old 03-05-2013, 11:52 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
BTW, the above could be made much cleaner. At the very least the two separate pattern matches can be combined into one: You can also fix it to match the first character directly instead of using the parameter substitution to extract it first.

Code:
if [[ ${REPLY:0:1} = [yY] ]]; then

if [[ $REPLY = [yY]* ]]; then

if [[ ${REPLY,,} = y* ]]; then
if [[ ${REPLY^^} = Y* ]]; then
But also, this kind of pattern match should usually be done with a case statement instead. It's nearly always faster, cleaner and more efficient than an if/test construct, and you can included as many conditions as you want.

Code:
case ${REPLY,,} in
    y*) echo "you selected yes" ;;
    n*) echo "you selected no"  ;;
     *) echo "what the heck is that?" ;;
esac
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
webserver doesn't reply to external request but it reply's to local request ziba Linux - Server 4 05-11-2009 05:27 PM
Quick Reply "post reply" button. IsaacKuo LQ Suggestions & Feedback 9 12-04-2005 03:41 PM
please reply sukhbhela Solaris / OpenSolaris 1 09-19-2004 03:50 AM
post reply & submit reply buttons annehoog LQ Suggestions & Feedback 10 01-05-2004 06:43 PM
Evolution - reply - missing own reply emanuelgreisen Linux - Software 0 01-31-2003 04:40 AM


All times are GMT -5. The time now is 08:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration