Appending variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending variables
# 1  
Old 11-27-2008
Appending variables

Hi,

I'm having a problem with something I can't really figure out by myself. I've tried to do it with a for loop, but I'm pretty sure it won't work, if I'm not mistaken.

Basically, what I'm trying to do is test some variables and if any of them is not empty, add the values of these variables to the list. For example:

Code:
sub1="subtitle1.idx"
sub2=
sub3="subtitle3.idx"

for i in "$sub1" "$sub2" "$sub3"; do
    MP4SUBS="-add $i"
done

MP4Box -add video.h264 -add audio.aac $MP4SUBS

This does not work with a for loop, as you know. I know this can be done with an if conditional, but how do I deal if I have lots of sub* variables? An if conditional is not really recommended here, is it? Any suggestion how to do this in a simple way, if possible?

Thanks.
# 2  
Old 11-27-2008
Hi,

say you have $sub1 up to $sub10.

Code:
mp4="" && for i in {1..10}; do [[ -z $(eval "echo \$sub$i") ]] && echo empty || mp4="$mp4 -add $(eval "echo \$sub$i")"; done

Now $mp4 should contain the value of each non-empty variable from sub1 up to sub10.

[[ -z ]] tests if something is of value zero. If it is, echo empty, if not append it to your variable. You have to use eval to construct the variable name.

This is a really messy solution. The next time you should use an array.

HTH Chris
# 3  
Old 11-27-2008
Ok, thanks. This seems to work. Any example on how to use it with an array? I've never used array's in Bash so I'm a clueless noob here.
# 4  
Old 11-27-2008
It works but is really ugly. This one is much simpler.

Code:
mp4="" && for i in ${!sub*}; do mp4="-add ${!i} $mp4"; done

It uses a technique called indirection. That's the ! in the ${!..}s.

If you are interested in arrays, you must post a complete example
of what you have and what you want.

For a detailed explanation take a look at the Advanced Bash
Shell Scripting Guide under:

The Linux Documentation Project: Guides

HTH Chris
# 5  
Old 11-27-2008
hmm, that works too but it adds the values backwards. Is this normal?
# 6  
Old 11-27-2008
Try to understand the code and think a little bit about it.
Take a closer look at both snippets and experiment with
them.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

BASH arrays and variables of variables in C++

Sometimes it is handy to protect long scripts in C++. The following syntax works fine for simple commands: #define SHELLSCRIPT1 "\ #/bin/bash \n\ echo \"hello\" \n\ " int main () { cout <<system(SHELLSCRIPT1); return 0; } Unfortunately for there are problems for: 1d arrays:... (10 Replies)
Discussion started by: frad
10 Replies

4. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

5. Shell Programming and Scripting

Need help on appending line..

Hi Friends , I got struck in below scenario. Require help to get the desired output. I was going to other blog "Need help on appending all the lines in a file after a pattern is found." but was not able to make with respect to my requirement. Output Getting : -- Storage Group Name: ... (5 Replies)
Discussion started by: Vivek Iyer
5 Replies

6. Shell Programming and Scripting

Matching and appending

I have two files FILEA a/b/c/d e/f/g/h i/j/k/l m/n/o/p FILEB i/j/k/l i want that if lines of FILEB matches lines of FILEA then matching lines in FILEA to be appended by word "MATCH" NEW FILEA a/b/c/d e/f/g/h i/j/k/l "MATCH" m/n/o/p I have tried following script .but not... (6 Replies)
Discussion started by: shaan4uster
6 Replies

7. Shell Programming and Scripting

Need assistance with appending strings using sed and variables

HI, Can't seem to find anything on the forums to fix this. I have a file, one line within this will not have a specific string at the end. I have the string, but need to append it to the specific line which has it missing. I need to use a variable for this, $string - I am using double... (13 Replies)
Discussion started by: mandriver
13 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. Shell Programming and Scripting

appending

hi, a1.txt ------ xyz zxc xya xdf a2.txt ------ a1 a2 b1 b2 cat a1.txt >> a2.txt -->its appending at the end (6 Replies)
Discussion started by: mohan705
6 Replies

10. Shell Programming and Scripting

appending

i've been fiddling around with append and I know how to append a word into a file, but how would you go about appending a word to a filename? (5 Replies)
Discussion started by: codewarrior7
5 Replies
Login or Register to Ask a Question