Creating variables in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating variables in bash
# 8  
Old 04-19-2012
I would create a function for that
# 9  
Old 04-19-2012
How would this function work exactly?
# 10  
Old 04-20-2012
Well something like this to give you an idea:
Code:
RED="\e[0;31m"
BLUE="\e[0;34m"
GREEN="\e[0;32m"
NOCOLOR="\e[0m"

formatprint() {
  printf "${3}%${2}s\n${NOCOLOR}" "$1"
}

formatprint "Hello World"
formatprint "Hello World" 16
formatprint "Hello World" 24
formatprint "Hello World" 30 "$RED"
formatprint "Hello World" 36 "$GREEN"

If you are using bash 4 or ksh93 (my favorite for shell scripts) you can use associative array elements for the colors instead of variables

Last edited by Scrutinizer; 04-20-2012 at 02:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 11  
Old 04-20-2012
Thank you for this. Associative arrays are very nice, but cannot use them when running my scripts on some clusters I have available, which is a pity.

---------- Post updated at 05:45 AM ---------- Previous update was at 05:12 AM ----------

As an addition, I am constructing some mnemonic keywords from which I can quickly extract information. I use three attributes: s, v, and d.

Mnemonic examples:
Code:
1s             one option
2s             two options
3s             three options
1sv           one option with value
1s2v         one option with two values
2sv           two options with value
2svd         two options with value and description

I want to determine the total number of attributes in the string as follows:
Code:
1s              nAtrb=1
2s              nAtrb=2
3s              nAtrb=3
1sv            nAtrb=2
1s2v          nAtrb=3
2sv            nAtrb=3
2svd          nAtrb=4


Last edited by kristinu; 04-20-2012 at 07:51 AM..
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