Sponsored Content
Full Discussion: Bash Parameter Expansion
Top Forums Shell Programming and Scripting Bash Parameter Expansion Post 303013629 by bakunin on Saturday 24th of February 2018 03:12:30 PM
Old 02-24-2018
Quote:
Originally Posted by kristinu
Is there a way to get the filename without the directory and without the extension, using parameter expansion to get

Code:
  iv.hpac..hhz.d.2016.250.070018

Yes, but it would be cumbersome and plain ugly. Writing scripts (i suppose you need it in a script) is all about clarity and making it easy to understand what is done, therefore you should do it in two steps using the expansions you already found. I would write it like this:

Code:
text="${text##*/}"      # cut off the path
text="${text%.*}"       # cut off the extension

For completeness' (but again: don't really do it! This is just to show off my prowess in writing incomprehensible code! ;-) ) sake: you can use an expansion inside another expansion as a pattern. For instance, this cuts off the first character in a string, notice the nested expansion:

Code:
chLine="abcdefghi"

while [ -n "$chLine" ] ; do
     chChar="${chLine%${chLine#?}}"        # nested expansion!!
     chLine="${chLine#?}"

     echo char is: \"$chChar\"
     echo line is: \"$chLine\"
done

Using this device you could do what you want. Don't!

I hope this helps.

bakunin
These 2 Users Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

removing html tags via parameter expansion

Hi all- I have a variable that contains a web page: echo $STUFF <html> <head> <title>my page</title></head> <body> blah blah etc.. Can I use the shell's parameter expansion abilities to remove just the tags? I thought that FIXHTML=${STUFF//<*>/} might do it, but it didn't seem to... (2 Replies)
Discussion started by: rev66
2 Replies

2. Shell Programming and Scripting

Need help with parameter expansion

Say you have this numeric variable that can be set by the user but you never want it to leave a certain range when it gets printed. How could you use parameter expansion such that it will never expand outside of that boundary? Thanks ---------- Post updated at 11:09 PM ---------- Previous update... (3 Replies)
Discussion started by: stevenswj
3 Replies

3. Shell Programming and Scripting

Bash parameter expansion from a config file

Hi - I am trying to do a simple config file with known variable names in it, e.g.: contents of config file a.conf: -a -b $work -c $host simplified contents of bash script file: work='trunk' host='alaska' opts=$(tr '\n' ' ' < a.conf) opts="$opts $*" mycommand $opts arg1 arg2 The... (3 Replies)
Discussion started by: mrengert
3 Replies

4. Shell Programming and Scripting

Parameter expansion not working for all strings...

I'm trying to write a script that parses my music collection and hard link some filenames that my media player doesn't like to other names. To do this I need to extract the name and remove alla non ASCII characters from that and do a cp -l with the result. Problem is this: 22:16:58 $... (8 Replies)
Discussion started by: refuser
8 Replies

5. UNIX for Dummies Questions & Answers

Parameter Expansion with regular expression

Hello experts, I am exploring parameter expansion, and trying to cut the fields in a URL. Following is the requirement: I have // abc.nnt /dir1/dir2/dir3/dir4/somefile.java What i need to get is the path after dir3, and dir3 will be passed. output that i need is... (1 Reply)
Discussion started by: gjarms
1 Replies

6. Shell Programming and Scripting

Bash expansion

Hello. I cannot write a command without using eval. Any help is welcome Note 1 : What does the function SOMETHING has no importance. Note 2 : What does the command find has no importance. It is an expansion variable problem : where to put or or or anythings else What works (FILTRE_1... (8 Replies)
Discussion started by: jcdole
8 Replies

7. Shell Programming and Scripting

Bash Parameter Expansion

#!/bin/bash SNMPW='/usr/bin/snmpwalk' while read h i do loc=$($SNMPW -v3 -u 'Myusername' -l authPriv -a SHA -A 'Password1' -x AES -X 'Password2' $i sysLocation.0 2>/dev/null) loc=${loc:-" is not snmpable."} loc=${loc##*: } loc=${loc//,/} echo "$i,$h,$loc" done < $1 My question is ... ... (1 Reply)
Discussion started by: sumguy
1 Replies

8. Shell Programming and Scripting

Bash : More parameter expansion and IFS

I am trying to become more fluent with the interworking of bash and minimize the number of external calls. Sample Data. This will be the response of the snmp query. SNMPv2-MIB::sysName.0 = STRING: SomeHostName SNMPv2-MIB::sysObjectID.0 = OID: SNMPv2-SMI::enterprises.9.1.1745... (5 Replies)
Discussion started by: sumguy
5 Replies

9. Shell Programming and Scripting

Bash : Parameter expansion ${var:-file*}

Example data $ ls *somehost* 10.10.10.10_somehost1.xyz.com.log 11.11.11.11_somehost2.xyz.com.log #!/bin/bash #FILES="*.log" FILES=${FILES:-*.log} for x in $FILES do ip="${x%%_*}" # isolate IP address x="${x##*_}" # isolate hostname hnam="${x%.*}" # Remove the ".log"... (2 Replies)
Discussion started by: popeye
2 Replies

10. Shell Programming and Scripting

Use parameter expansion over a parameter expansion in bash.

Hello All, Could you please do help me here as I would like to perform parameter expansion in shell over a parameter expansion. Let's say I have following variable. path="/var/talend/nat/cdc" Now to get only nat I could do following. path1="${path%/*}" path1="${path1##*/}" Here... (8 Replies)
Discussion started by: RavinderSingh13
8 Replies
JMAKE(1)						      General Commands Manual							  JMAKE(1)

NAME
jmake - a generic makefile builder SYNOPSIS
jmake [ cpp options ] DESCRIPTION
Jmake builds a makefile out of a rather high level description held in a Jmakefile file. The generated file is a Makefile.SH rather than a simple makefile, which means it is ready to be used in conjonction with metaconfig. In particular, parameters such as "where to install executables" will be automatically determined by Configure and only the needed parameters will be taken into account. To use jmake you have to write a Jmakefile first, which describes the way things are to be built. Your Jmakefile will be included inside a generic template through the C pre-processor. This means you may use the usual C /**/ comments, but not the shell # comments. The C com- ments will not appear in the generated Makefile.SH but lines starting with ;# will finally appear as shell comments. If you have to write the string /* in the generated Makefile.SH then you have to escape it (otherwise jmake will think of it as the start of a C comment). Sim- ply put a # in front of the *, as in /#*. You have a set of macros at your disposal, and all these macros are listed in the Index file, along with the piece of code they will expand to. Usually, a Jmakefile is fairly small and thus easier to maintain than a huge Makefile. Some internal powerful commands allow you to write portable makefiles easily, without having to spend many efforts, because someone else already did the job for you :-). When you want to generate your makefile, you usually do not run jmake but use the jmkmf script which is a wrapper and will invoke jmake with the correct options. All the knowledge of jmake is held in two files: the template Jmake.tmpl and the macro definition file Jmake.rules. The first file includes the second, along with the Jmakefile. It is sometimes necessary to know how things works to be able to correctly use all the fea- tures provided. For instance, you may have to write your own rules for a specific project. Although you cannot overwrite the predefined rules, you can extent the Jmake.rules file or simply add your macros in your Jmakefile. You may also use #include statements when you want to share these macros and do not want to duplicate the code. The syntax in Jmake.rules is not elegant at all, but: - It is easy to parse (like sendmail.cf or troff files). - The rules are not supposed to change very often. - It is simple enough to be mastered in five minutes. :-) Here is a small description: 1) To deal with various cpp implementations: o Final @! means: end of line, next line starts at the left margin. o Final @@ means: end of line, next line is to be indented by one tab. There should always be one of @! or @@ at the end of each line. The only exception is for macros that are to be used as part of a rule body (e.g. RemoveTargetProgram). In that case, the first line (which holds the #define) should end with a single backslash. 2) Symbol definitions: o >SYMBOL: defines the symbol. o ?SYMBOL:<text>: keeps <text> iff SYMBOL is defined. o %SYMBOL:<text>: keeps <text> iff SYMBOL is not defined. The ?SYM can be nested (logical AND), as in: ?SYMBOL:%TOKEN:text which will keep text if SYMBOL is defined and TOKEN undefined. To implement a logical OR, see below. 3) Commands: Commands can be passed to jmake. They start with a leading '|'. Available commands are: o |suffix <sx>: adds <sx> to the .SUFFIXES: list in the makefile. o |rule:<text>: adds <text> to the building rule section. o |rule: <text>: same as before, with a leading tab. o |skip: skips text until a line starting with '-skip' is found. o |subst: begins section where lines will be subject to variable substitution, until '-subst' is found. This means that when the Makefile.SH is run, all instances of $var within the subst section will be substituted by the shell. o |shell: emits section until matching '-shell' as-is in the generated Makefile.SH. This can be useful to prepare |case sec- tions. It is not allowed to nest shell sections. o |case: this command must be followed by a shell variable name (without its leading '$' sign) and a case-style pattern, for instance the string "var in f*". It will generate the corresponding "case" test in the Makefile.SH on the "$var" value and only if this test is true will the section until the matching '-case' be generated in the Makefile when Makefile.SH is run. It is possible to nest case sections freely. o |expand <pattern>: expand lines until '-expand' with <pattern>. A complete example is shown below. o |once <symbol>: text up to '-once' appears only the first time. The '|' commands cannot be nested, unless otherwise noted. In particular, due to the simple implementation of |skip, it is impossi- ble to put |skip inside a skipped part. However, a |once section may have |skip sections. It is allowed to nest |case sections at will. Here is a way to implement a logical OR: /* Implements SYMBOL or not TOKEN */ ?SYMBOL:text /* Keeps text if SYMBOL */ %SYMBOL:|skip %TOKEN:text /* Keeps text if not TOKEN */ -skip Actually, this is ugly, because the text has to appear twice. Fortunately, I did not use it. :-) But actually, as you have surely already guessed, the best way to implement a logical OR is to use De Morgan's Law: not (p or q) <=> not p and not q /* Implements SYMBOL or not TOKEN (attempt #2) */ %SYMBOL:?TOKEN:|skip text /* If SYMBOL or not TOKEN */ -skip Who said they didn't care ? ;-) Expansion is done with the expand command. It has been provided to avoid some cumbersome writings in makefiles when you have to repeat some silly lines that only differ in file names, for instance. Let's look at an example first: |expand a!foo bar! b!yes no! !a:: echo !a, !b -expand Then two rules will be printed, and the values of (a,b) for the first will be (foo, yes), for the second (bar, no). Substitution is controled by the '!' character. If the word to be substituted is part of another one, detach with the ^^ construct as in: !b^^c. It is possible to use Makefile macros in the <pattern>, and they will be expanded by jmake. If this is not what you want, escape the first '$' sign (this is a Makefile escape, i.e. you must double the '$', not precede it with a backslash). A // stands for the null substitution value. The ^^^ construct behaves like ^^, i.e. it is stripped out, but it also removes any following white space after the ^^^. If you prepend something to a macro argument, and that macro argument was written with spaces before it, then this will let you concatenate something right before that argument's final value. Here is another example which shows how the macro Expand can be used. It is defined in Jmake.rules as: #define Expand(rule, pattern) @! |expand pattern @! rule @! -expand So we can write in the Jmakefile: |skip A = foo bar -skip #define Rule @! $(DIR)/!a^^.o: !a^^.o @@ $(CC) -c !a^^.c @@ $(MV) !a^^.o $(DIR) Expand(Rule, a!$(A)!) which will generate in Makefile.SH: $(DIR)/foo.o: foo.o $(CC) -c foo.c $(MV) foo.o $(DIR) $(DIR)/bar.o: bar.o $(CC) -c bar.c $(MV) bar.o $$(DIR) The 'A' declaration has been surrounded by skip, so that it does not appear in the generated Makefile.SH, but it will be taken into account by jmake for the substitution in the pattern. The number of expansions is determined by the number of possible values for the first parameter. If other parameters have less sub- stitution values, they will get void ones. It is possible to add a regular expression at the end of '-expand'. This regular expression will be removed from the final set of expansion at the end of each line. It is also possible to do substitutions in the expanded item, by using the syntax (if 'f' is the expanded variable) !f:<p>=<q> where <p> and <q> are two regular expressions (without spaces). The pattern <p> will be replaced by the pattern <q> (only the first occurrence will be replaced). Finally, you may refer in the expanded section to variables whose value is computed via another expansion, which makes it easy to define generic Jmakefiles. Example: SRC = foo.c bar.c OBJ = |expand f!$(SRC)! !f:.c=.o -expand \ INC = |expand f!$(OBJ)! !f:.o=.h -expand \ which will generate in Makefile.SH: SRC = foo.c bar.c OBJ = foo.o bar.o INC = foo.h bar.h Do not forget to protect special characters in your regular expressions such as backslash, point, etc... The once command is tagged with a name. The first time the name appears, the once construct is ignored and the text up to '-once' will be copied in the generated Makefile.SH. However, future occurences of the same name will be ignored (once will behave like skip). Example: |once this_is_a_name <text> -once The shell command can be used to generate a shell fragment in the Makefile.SH. For instance, the following section in the Jmake- file: |shell case "$d_usegtk1" in define) glib=1; gtk=1;; esac -shell will cause the generation of the enclosed fragment in the Makefile.SH to compute the values of the glib and gtk variables based on the configuration variable d_usegtk1 set by running Configure. In turn, this can be used in subsequent case sections to activate parts of the Makefile only when building for GTK1 using glib-1.x: |case glib in 1 display: echo "Building for glib-1.x" -case This section will generate something like this in the Makefile.SH: !NO!SUBS! case "$glib" in 1) $spitshell >>Makefile <<'!NO!SUBS!' display: echo "Building for glib-1.x" !NO!SUBS! ;; esac $spitshell >>Makefile <<'!NO!SUBS!' And when running Makefile.SH, the "display" rule above will only appear when building for glib-1.x. The form of the final Makefile can therefore depend on the configuration options chosen when Configure was run. 4) Initializations: o +<line>: Puts the whole line in the initialization section. o ++SYMBOL <value>: Adds <value> to the SYMBOL macro. 5) User-defined variables: The user may define CFLAGS, LDFLAGS or DPFLAGS as additional flags to be used in C compilation, linking phase or depend target. It is thus possible to add some extra flags such as -I or libraries for Makefiles in specific sub-directories. AUTHOR
Raphael Manfredi <Raphael_Manfredi@pobox.com> FILES
Jmakefile High level description of Makefile.SH Jmake.rules File holding the macro definitions Jmake.tmpl Template used to mould Makefile.SH BUGS
On systems whose cpp reduces multiple tabs and spaces to a single space, jmake attempts to put back any necessary tabs (which make expects in front of rules) but does not properly formats the body of the rule itself. There is a bootstraping problem when creating the first Makefile.SH, because you cannot run it through a shell until there is a decent Con- figure script, but you can't run metaconfig before there is a Makefile.SH or some needed symbols will not be defined. SEE ALSO
jmkmf(1), metaconfig(1). ram JMAKE(1)
All times are GMT -4. The time now is 03:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy