Bash assign string to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash assign string to variable
# 22  
Old 02-03-2011
I suggested that the OP discard let ( I've noticed that sometimes people use this thinking it is a regular assignment, perhaps because of previous experience with BASIC ). I pointed out that it is used for arithmetic purposes.

If the OP is aware of this and wants to use it then that is fine of course, but it can't hurt to note that in that case the script will only run in some shells, whereas $(( .. )) is portable. I agree with cfajohnson, that there is no advantage in using let (and there is a disadvantage).
# 23  
Old 02-03-2011
Quote:
Originally Posted by ghostdog74
Whatever it is, the OP did not say he wants or not to do POSIX mode. The title also specifically said "Bash".
Using "let" the way he uses it is not "wrong" either, since its supported in Bash, (although I do agree it's not necessary.)

The way he used it is wrong. He was using it to assign a string.

In any case, I do not recommend bad practices to people just learning the shell. One may be using bash now, but in the future may have to use a shell that doesn't support let in any context. If it added something to the script, there would be some justification for it, but it doesn't.
# 24  
Old 02-03-2011
Quote:
Originally Posted by cfajohnson

The way he used it is wrong. He was using it to assign a string.

if the assigned string after his grepping and cutting is a number, then it is still technically not wrong. Some of them like qty and price is technically correct. Of course assuming that what he really grepped is the correct number (Note the word "technically")

# 25  
Old 05-02-2011
Am trying to change all of my csh scripts to bash and having trouble assigning grep/gawk output to string variable.

1. i have a textFile that looks like this:
Code:
sub1001_0_1_2_3_4_5
sub1002_0_1_2_3_4_5
sub1003_0_1_2_3_4_5

2. I want to grep $subNum in $textFile and assign a variable to be the concatenation of the last 5 columns, with the underscores removed:

eg 012345

3. In csh I had:

Code:
#!/bin/csh
set $textFile = groupIDs.txt
foreach subNum (1001 1002 1003)
   set groups = `grep $subNum $textFile |gawk -F "_" '{print $2,$3,$5,$5,$6}'`
end

4. In bash I have:

Code:
#!/bin/bash
textFile="groupIDs.txt"
for subNum in 1001 1002 1003
   groups=`grep $subNum $textFile |gawk -F "_" '{print $2,$3,$5,$5,$6}'`
end

which obviously doesn't work. what syntax should I be using to assign the values in the last 5 columns to $group?

thanks so much - am new to bash. -d

Last edited by Franklin52; 05-03-2011 at 02:57 AM.. Reason: Please use code tags
# 26  
Old 05-02-2011
Code:
#!/bin/bash
textFile="$HOME/txt"
for subNum in 1001 1002 1003
do
   groups=`grep "$subNum" "$textFile" | gawk -F "_" '{print $2 $3 $5 $5 $6}'`
   printf "%s\n" "$groups"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using read to assign value to bash variable not working

Hi, I am attempting to assign the output of the following command, to two bash variables, var1 and var2 using "read," but it doesn't seem to be working. # openstack hypervisor stats show | awk -F'|' 'NR==14{print $2,$3}' vcpus 92 # echo $? 0 # openstack hypervisor... (4 Replies)
Discussion started by: sand1234
4 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. Shell Programming and Scripting

Assign variables to CSV string (bash)

Hi guys, New to the forum, and been messing around with Linux for about a year now. I'm still very much a rookie, so just assume that I'm a total idiot: I currently have a shell that spits out a CSV number string of about 8 numbers as follows: 1.00,2.00,3.00 ... ,8.00I need to assign a... (7 Replies)
Discussion started by: hansol
7 Replies

4. Shell Programming and Scripting

Assign a variable the nth character of a string.

I see a millioin ways to do this with echo, but what I wan to do is assign a variable the "nth" character of an incoming parameter to a ksh script. $1 will be "pia" I need to assign the first character to stmttype. (10 Replies)
Discussion started by: klarue
10 Replies

5. Shell Programming and Scripting

need to assign a string to a variable

Hello Experts, In my script i am using the below syntax /usr/bin/head -1 /opt/chumma.txt | /usr/bin/cut -d " " -f3 output of this one is --> Monday I want to store this String in a variable (i.e) j or k... Give me some idea experts. Thanks in advance. (7 Replies)
Discussion started by: natraj005
7 Replies

6. Shell Programming and Scripting

bash assign mysql query single field to variable

I'm running a bash script query and assigning the output to a variable like this: exists=`mysql -u $USER_NAME --password=$PASSWORD -D "somedb" \ -e "SELECT * FROM somedb.sometable WHERE field1 ='$a' \ AND field2 ='$b' LIMIT 0 , 30";` which returns something like: echo... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

8. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

9. Shell Programming and Scripting

read and assign each character from the string to a variable

How... can I read input by a user character by cahracter. And assign each character from the string to a variable? Any help would be greatly appreciated! Thank you! (1 Reply)
Discussion started by: Tek-E
1 Replies

10. Shell Programming and Scripting

How to assign variable from a string having blanks in between

Hi All, I am new to bash scripting. I need your help to removing spaces from a string and assign them to different variables. Iwant to call script with one command line argument which is file name which containes different attributes their type and different values eg ... (1 Reply)
Discussion started by: flextronics
1 Replies
Login or Register to Ask a Question