Sponsored Content
Top Forums Shell Programming and Scripting Real-time scenarios where VARIABLE SUBSTITUTION/EXPANSION is useful Post 302946075 by bakunin on Saturday 6th of June 2015 05:28:45 PM
Old 06-06-2015
Quote:
Originally Posted by ab_2010
at academic level I am familiar with how variable substitution/expansion feature works. From your live experience, can you please tell what are real-time scenarios where ${variable:=} ${variable%pattern} ${variable:=word} sort of features can be used?
First off, I think it is one of the defining qualities of a software engineer to come up with useful applications of a language feature once he gets to know it. By analogy, Beethoven had the same 12 tones to work with as any other but what made him a composer was that he started having ideas on how to combine them in an original and inventive way.

Here are, off the top of my head, some threads where i mentioned applications for variable expansion:

walking back up a directory path

Storing string space

Not to forget the obvious application:

Code:
var="${othervar:=defaultvalue}"

which assigns a variable a default value in case "$othervar" is not set so far. This way you can avoid lengthy checks for completeness of sets of input parameters.

Finally, you can use variable expansion for everything where you would normally use string functions in other languages or string-manipulating commands (grep, awk, sed, tr, ...) in shell. Variable expansion is very fast and even if the code is ten times as long it will perhaps be executed faster than any external program. I learned this lesson myself right here.

I hope this helps.

bakunin
 

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

2. Shell Programming and Scripting

[Solved] Command Substitution and Variable Expansion within a Case

Hello All, I don't write scripts very often, and in this case I am stumped, although it may be a bug in the version of bash I have to use (it's not my system). I want to extract a specific string snippet from a block of text (coming from a log file) that is dependent on a bunch of other... (1 Reply)
Discussion started by: jaimielives
1 Replies

3. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

4. Shell Programming and Scripting

Converting real time to epoch time

# date +%s -d "Mon Feb 11 02:26:04" 1360567564 # perl -e 'print scalar localtime(1360567564), "\n";' Mon Feb 11 02:26:04 2013 the epoch conversion is working fine. but one of my application needs 13 digit epoch time as input 1359453135154 rather than 10 digit epoch time 1360567564... (3 Replies)
Discussion started by: vivek d r
3 Replies

5. Shell Programming and Scripting

sed variable expansion fails for substitution in range

I'm trying to change "F" to "G" in lines after the first one: 'FUE.SER' 5 1 1 F0501 F0401 F0502 2 1 F0301 E0501 F0201 E0502 F0302 3 1 F0503 E0503 E0301 E0201 E0302 E0504 F0504 4 1 F0402 F0202 E0202 F0101 E0203 F0203 F0403 5 1 F0505 E0505 E0303 E0204 E0304 E0506... (10 Replies)
Discussion started by: larrl
10 Replies
DWWW(1) 							      Debian								   DWWW(1)

NAME
dwww - access documentation via WWW SYNOPSIS
http://localhost/dwww/index.html dwww dwww program-name | package-name DESCRIPTION
A typical Linux system has documentation in many formats (manual pages, info files, READMEs, and so on). dwww makes it possible to access all of these via the same interface, a WWW browser. This makes it easier to use the documentation. To use dwww, load the URL given in the SYNOPSIS. If you have a web browser installed, you can also just run the dwww command which loads the URL. If BROWSER environment variable is set, dwww uses sensible-browser(1) to load the URL. Otherwise, dwww first tries to use browser specified by DWWW_BROWSER or DWWW_X11_BROWSER configuration variable (cf. dwww(7)), than will use sensible-browser(1) command. If optional argument program-name or package-name is specified, dwww will search all documentation related to given program or package. ENVIRONMENT
BROWSER Program used to load the above mentioned URL. FILES
/etc/dwww/dwww.conf Configuration file for dwww. SEE ALSO
dwww-find(8), dwww(7), sensible-browser(1). AUTHOR
Lars Wirzenius <liw@iki.fi>. Modified by Robert Luberda <robert@debian.org>. Bugs should be reported via the Debian Bug Tracking System at <URL:http://bugs.debian.org/>. dwww is licensed via the GNU General Public License. While it has been written for Debian, porting it to other systems is strongly encour- aged. dwww 1.11.1 February 15th, 2009 DWWW(1)
All times are GMT -4. The time now is 10:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy