Creating variables in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating variables in bash
# 1  
Old 04-19-2012
Creating variables in bash

I am writing some scripts using bash and am wondering if there is a better way to perform the following set of formatting variables.

Code:
s1=" "
s2="  "
s3="   "
s4="    "
s5="     "
s6="      "
s7="       "
s8="        "
 
frmt_titl="${bYl}%s${nClor}\n"

frmt1_titl="${s1}$frmt_titl"
frmt2_titl="${s2}$frmt_titl"
frmt3_titl="${s3}$frmt_titl"
frmt4_titl="${s4}$frmt_titl"
frmt5_titl="${s5}$frmt_titl"
frmt6_titl="${s6}$frmt_titl"
frmt7_titl="${s7}$frmt_titl"
frmt8_titl="${s8}$frmt_titl"

nfrmt_titl="\n$frmt_titl"
nfrmt1_titl="\n$frmt1_titl"
nfrmt2_titl="\n$frmt2_titl"
nfrmt3_titl="\n$frmt3_titl"
nfrmt4_titl="\n$frmt4_titl"
nfrmt5_titl="\n$frmt5_titl"
nfrmt6_titl="\n$frmt6_titl"
nfrmt7_titl="\n$frmt7_titl"
nfrmt8_titl="\n$frmt8_titl"

# 2  
Old 04-19-2012
Try this:
Code:
$ w=12; printf "%${w}s\n" hello
       hello
$ w=20; printf "%${w}s\n" hello
               hello

# 3  
Old 04-19-2012
It looks interesting. I will be doing something as shown below:

Code:
  printf "$frmt4_titl" "NAME"

which gives yellow color text with 4 spaces in the front

I do not want to have a long line. The idea is that I write relatively short format string, followed by the things I want to print.

I essence, I want to construct the format variables above in a better way.

---------- Post updated at 03:26 PM ---------- Previous update was at 03:22 PM ----------

Can I construct a variable name with a counter using a loop?

---------- Post updated at 03:34 PM ---------- Previous update was at 03:26 PM ----------

Quote:
Originally Posted by Scrutinizer
Try this:
Code:
$ w=12; printf "%${w}s\n" hello
       hello
$ w=20; printf "%${w}s\n" hello
               hello

I have been thinking a bit more and variable names in shell are not designed to be dynamically created. So I suppose I would need to keep the code not much different.
# 4  
Old 04-19-2012
You can use eval (or indirect references in bash/ksh93), but you need to be careful with security, especially with user input or input files...

Code:
$ for i in 1 2 3; do eval var$i=$((i*10)); done
$ echo $var1 $var2 $var3
10 20 30

# 5  
Old 04-19-2012
What do you think of using

Code:
export

Example

Code:
i=4
s4="    "
frmt_titl="${bYl}%s${nClor}\n"
export frmt${w}_titl="${s4}$frmt_titl"




---------- Post updated at 03:46 PM ---------- Previous update was at 03:44 PM ----------

Quote:
Originally Posted by Scrutinizer
You can use eval (or indirect references in bash/ksh93), but you need to be careful with security, especially with user input or input files...

Code:
$ for i in 1 2 3; do eval var$i=$((i*10)); done
$ echo $var1 $var2 $var3
10 20 30

I am planning to define all this stuff in a bash file. Then source it, so that I can use the variables from another script.

---------- Post updated at 03:49 PM ---------- Previous update was at 03:46 PM ----------

Code:
Code:
cat utility.bash

#!/bin/bash
for i in 1 2 3; do eval var$i=$((i*10)); done
Code:
cat test.bash

#!/bin/bash
source utility.bash
echo "$var1"

# 6  
Old 04-19-2012
What's wrong with using printf? You can tell it how wide to make a field, instead of stuffing 20 spaces in one string, 19 in another, 18 in another...
# 7  
Old 04-19-2012
There is nothing wrong with using printf. In fact I will use it. What I am trying to do is specify the format to printf. Then in my main script I call printf followed by the format string, followed by the strings to output. Else things will get too long if I put the actual format.

Example
Code:
printf "format" "str1" "str2" "str3" "str4" "str5"

The thing is that the output of str1 str2 str3 might also be of different colors which makes the format specification very long

As an example, the example might print as

str1, str2, str3, str4 str5
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Homework & Coursework Questions

Creating a .profile, displaying system variables, and creating an alias

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Here is what I am supposed to do, word for word from my assignment page: 1. Create/modify and print a... (2 Replies)
Discussion started by: Jagst3r21
2 Replies

4. Shell Programming and Scripting

Creating .../ alias in bash

I want to create an alias as follows but is not working alias ../='cd ../' (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Creating Dynamic Variables from a Flat File

Greetings all, Been trying to do my Googling and forum searches but can't seem to lock in on a solution. I have a script that parses a log and collects all the uniq events to a flat file. Some days might have 50 unique events, other days might have 75. (Hence my reference to dynamic.) ... (2 Replies)
Discussion started by: sjrupp
2 Replies

6. Linux

Creating 2 variables from a multiple pattern grep

first time poster here Im pretty new to grep and linux in general and I spent pretty much all day yesterday researching and coming up with a grep command to help with my university project. I am attempting to create a proof of concept bash script to scan the network using ngrep, find appropriate... (7 Replies)
Discussion started by: scottish_jason
7 Replies

7. Shell Programming and Scripting

need help with creating directories and variables

i'm trying to write a script that has 2 variables, and uses the 1st variable as a number and the 2nd a name to create directories. so if you typed in ./myscript 5 week, it would create 5 directories named week1 - week5. whenever i run this, i get an error message saying week5 already exists, so i... (3 Replies)
Discussion started by: layne2kim
3 Replies

8. Shell Programming and Scripting

Creating variables dynamically and using it in script?

Hi, I have a problem that I am trying to solve and would greatly appreciate some input to solve this. I have a file containing variable length of line. Each line in the file has values separated by "," and i need to grep for these values in a some files. For example below is a sample file with 3... (12 Replies)
Discussion started by: davidtd
12 Replies

9. Solaris

Creating script adding 3 different variables in 3 columns

I have 3 variables with different information.. they look like this (row-wise aswell): Variable1 = Roland Kalle Dalius Variable2 = ake123 ler321 kaf434 Variable3 = Richardsen Sworden Lokthar How can I sort them by variable3 alphabetical and add them into the same output so... (0 Replies)
Discussion started by: Prantare
0 Replies

10. Shell Programming and Scripting

help needed with creating challenging bash script with creating directories

Hi, Can someone help me with creating a bash shell script. I need to create a script that gets a positive number n as an argument. The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
Discussion started by: I-1
7 Replies
Login or Register to Ask a Question