LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 08-24-2010, 01:33 PM   #1
manikandan_vs
LQ Newbie
 
Registered: Jan 2010
Posts: 20

Rep: Reputation: 0
Red face how to append a char or strong


i`am new to shell scripting , just few days before only started to learn......

the query is ||| how to append a char | string |||

#!/bin/bash

echo -n "one"\"two"\"three"

#END


above method is working well but below method is not working ????
pls help me friends.....


#!/bin/bash

one=one
two=two
three=three

echo -n $one\$two\$three

#END

Last edited by manikandan_vs; 08-24-2010 at 01:35 PM.
 
Old 08-24-2010, 01:37 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Your post makes little sense; please explain in detail exactly what it is you're trying to do? And why are you escaping those quotes?

To append a char to a string in the shell, something like:
Code:
var="Hello ther"
var2="${var}e"
Now, $var2 contains what $var had contained, plus an 'e' on the end.
 
Old 08-24-2010, 01:51 PM   #3
manikandan_vs
LQ Newbie
 
Registered: Jan 2010
Posts: 20

Original Poster
Rep: Reputation: 0
Quote:
[mani@Mani shell]$ cat n_useradd.sh
#!/bin/bash
# Author: Manikandan
# Date: 17-08-2010
# Purpose: to create a no of users using FILE

processLine()
{
line="$@" # get all args
uf_name=$(echo $line | awk '{ print $1 }')
ul_name=$(echo $line | awk '{ print $2 }')
u_passwd=$(echo $line | awk '{ print $3 }')
u_shell=$(echo $line | awk '{ print $4 }')
u_dir=$(echo $line | awk '{ print $5 }')
r_num=$(((RANDOM%3000)*3197))
u_name=${uf_name:0:1}\"${ul_name:0:1}"\"${r_num}"
useradd $u_name -p $u_passwd -s $u_shell -d $u_dir
}

FILE=""

if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE=$1;
# make sure file exist and readable permissions
if [ ! -f $FILE ]; then
echo "$FILE: does not exists";
exit 1;
elif [ ! -r $FILE ]; then
echo "$FILE: can not read";
exit 2;
fi
fi

BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
# use $line variable to process line in processLine() function
processLine $line
done
exec 0<&3
# restore $IFS which was used to determine what the field separators are
IFS=$BAKIFS
exit 0

#END
while executing this file error occurs like this !!!!
so please help me friends......


Quote:
[mani@Mani shell]$ sudo ./n_useradd.sh users
useradd: invalid user name 'M"S"716128'
useradd: invalid user name 'K"S"1291588'
[mani@Mani shell]$

Last edited by manikandan_vs; 08-24-2010 at 02:09 PM.
 
Old 08-24-2010, 02:00 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
I'm not going to go to great length to determine why you're writing this script as you have - it looks rather over-complicated to me. However, simply re-writing the below line:
Code:
u_name=${uf_name:0:1}\"${ul_name:0:1}"\"${r_num}"
and doing it like this:
Code:
u_name="${uf_name:0:1}${ul_name:0:1}${r_num}"
appears to solve the problem. I tested it with some values to demonstrate the difference:
Code:
sasha@reactor: uf_name='john'
sasha@reactor: ul_name='smith'
sasha@reactor: r_num='1234'
sasha@reactor: echo ${uf_name:0:1}\"${ul_name:0:1}"\"${r_num}"
j"s"1234
sasha@reactor: echo "${uf_name:0:1}${ul_name:0:1}${r_num}"
js1234
sasha@reactor:
The bold stuff is from your original line; following that is my version, which produces a usable username (I'm figuring that the quotation marks are the problem.)
Sasha
 
Old 08-24-2010, 02:05 PM   #5
manikandan_vs
LQ Newbie
 
Registered: Jan 2010
Posts: 20

Original Poster
Rep: Reputation: 0
thanks GrapefruiTgirl .........
 
Old 08-24-2010, 02:06 PM   #6
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
You're welcome I hope that solves it for you. If so, you can mark the thread [SOLVED] using the Thread Tools menu above your first post.

Cheers!
 
  


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
Help fixing C++ invalid conversion from const char* to char primerib Programming 26 03-10-2010 07:56 PM
invalid conversion from `const char*' to `char' error holystinken Programming 7 11-23-2009 07:01 PM
error: invalid conversion from const char* to char* Dahakon Programming 1 08-31-2009 10:33 AM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 11:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration