LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to append a char or strong (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-append-a-char-or-strong-828225/)

manikandan_vs 08-24-2010 12:33 PM

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

GrapefruiTgirl 08-24-2010 12:37 PM

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.

manikandan_vs 08-24-2010 12:51 PM

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]$

GrapefruiTgirl 08-24-2010 01:00 PM

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

manikandan_vs 08-24-2010 01:05 PM

thanks GrapefruiTgirl .........

GrapefruiTgirl 08-24-2010 01:06 PM

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!


All times are GMT -5. The time now is 11:00 PM.