LinuxQuestions.org
Visit Jeremy's Blog.
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 12-07-2012, 10:52 AM   #1
janniez
LQ Newbie
 
Registered: Dec 2006
Posts: 12

Rep: Reputation: 0
Append the first letter in string1 to string2 after the read command.


Hi, please help. I am trying to write a script to append the first letter or string to another string after the read command

E.g. read -p "Enter the full name" FIRST LAST
#Here is the problem. I can't guess
Echo "Your result is:" [F]LAST

So
If I run the script like

Enter the full name: Robert Kelly
#Output should be
Your result is: RKelly.

Thx fir your help.
 
Old 12-07-2012, 11:30 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Code:
read -p "Enter the full name: "; SPLIT=(${REPLY}); echo "${SPLIT[0]:0:1}${SPLIT[1]}"
 
1 members found this post helpful.
Old 12-07-2012, 12:40 PM   #3
User\ Name=`echo $USER`
LQ Newbie
 
Registered: Oct 2012
Location: Oklahoma
Distribution: Red Hat, Ubuntu, Mac OS X
Posts: 25

Rep: Reputation: 4
I used a bit of unSpawn's code, but with some checks built in. The while loop is there to insure they enter something, the first if statement ensures they enter a first and last name, and the second if statement ensures that if they enter their middle name, it still grabs their last name.

Code:
#!/bin/bash
while [ -z "${REPLY}" ]; do
    read -p "Enter your full name: "
done
SPLIT=(${REPLY})
if [ -z ${SPLIT[0]} ] || [ -z ${SPLIT[1]} ]; then
    echo " You didn't enter your full name"
    exit
fi
fname=`echo ${SPLIT[0]}`
lname=`echo ${SPLIT[1]}`
if ! [ -z ${SPLIT[2]} ]; then
    lname=`echo ${SPLIT[2]}`
fi
echo "Your result is ${fname:0:1}${lname}"

Last edited by User\ Name=`echo $USER`; 12-07-2012 at 02:34 PM. Reason: Added another check.
 
1 members found this post helpful.
Old 12-07-2012, 01:42 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,651

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
Or a variation on a theme:
Code:
read -p "Enter the full name: " first last; echo "${first:0:1}$last"
 
3 members found this post helpful.
Old 12-08-2012, 11:30 PM   #5
janniez
LQ Newbie
 
Registered: Dec 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
Code:
read -p "Enter the full name: "; SPLIT=(${REPLY}); echo "${SPLIT[0]:0:1}${SPLIT[1]}"
Hi, Thank your for help, when i run these set of commands i get the following output: {SPLIT[0]:0:1}KELLY. IS there something wrong with it?

it's also the first time ive come across SPLIT and REPLY, are they builtin commands? i can not use man to read more.
thank you very much for your help

Last edited by janniez; 12-08-2012 at 11:32 PM.
 
Old 12-09-2012, 12:29 AM   #6
janniez
LQ Newbie
 
Registered: Dec 2006
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by User\ Name=`echo $USER` View Post
I used a bit of unSpawn's code, but with some checks built in. The while loop is there to insure they enter something, the first if statement ensures they enter a first and last name, and the second if statement ensures that if they enter their middle name, it still grabs their last name.

Code:
#!/bin/bash
while [ -z "${REPLY}" ]; do
    read -p "Enter your full name: "
done
SPLIT=(${REPLY})
if [ -z ${SPLIT[0]} ] || [ -z ${SPLIT[1]} ]; then
    echo " You didn't enter your full name"
    exit
fi
fname=`echo ${SPLIT[0]}`
lname=`echo ${SPLIT[1]}`
if ! [ -z ${SPLIT[2]} ]; then
    lname=`echo ${SPLIT[2]}`
fi
echo "Your result is ${fname:0:1}${lname}"
thx. I solved the problem and your help is good as well. i am a newbie, please explain to me the use of -z in the loops while and if.
thank you
 
Old 12-09-2012, 03:13 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,651

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
REPLY is the default variable for read.

SPLIT is a made up variable like 'var'

man test for information on -z and other options that can be used
 
Old 12-09-2012, 07:07 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
Quote:
Originally Posted by janniez View Post
Hi, Thank your for help, when i run these set of commands i get the following output: {SPLIT[0]:0:1}KELLY. IS there something wrong with it?
You forgot to copy it including the dollar sign: "${SPLIT[0]:0:1}${SPLIT[1]}"


Quote:
Originally Posted by janniez View Post
it's also the first time ive come across SPLIT and REPLY, are they builtin commands? i can not use man to read more.
Wrt REPLY default variable name and 'read' as a BASH builtin can be accessed with 'help read' when running BASH. SPLIT is an arbitrary variable name. Make it a habit of having uppercase variable names for readability and use names that denote their origin or use in the process.
 
Old 12-26-2012, 03:01 PM   #9
User\ Name=`echo $USER`
LQ Newbie
 
Registered: Oct 2012
Location: Oklahoma
Distribution: Red Hat, Ubuntu, Mac OS X
Posts: 25

Rep: Reputation: 4
Quote:
Originally Posted by janniez View Post
thx. I solved the problem and your help is good as well. i am a newbie, please explain to me the use of -z in the loops while and if.
thank you
-z means if the variable does not exist. Therefore, if you say
Code:
if ! [ -z ${variable} ]; then
    code
fi
The bang (!) means "not", so it's a double negative, basically saying if this variable does not NOT exist (if it exists), do (whatever).
http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html <== good info page here if that makes absolutely no sense.
 
  


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
[SOLVED] Bash - string1 replaces part of string2 on concat yzfr1 Programming 5 04-19-2012 12:53 PM
Bash script to check if string1 contains string2 Sebbern Linux - Newbie 5 12-15-2009 05:19 AM
awk: get lines starting from String1 and finish String2 frenchn00b Programming 9 10-29-2007 09:16 AM
Shell: string1 += string2. How to do this? jhwilliams Linux - Software 6 06-19-2007 02:49 PM
Search and Replace a string1 with string2 in many files TroelsSmit Linux - Newbie 5 12-16-2004 02:04 PM


All times are GMT -5. The time now is 10:14 PM.

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