HiiHahWIKI - making some notes for... -
Linux:シェルスクリプトでパス文字列からファイル名/ディレクトリ名を抽出する
をテンプレートにして作成
開始行:
*Linux:シェルスクリプトでパス文字列からファイル名/ディ...
これは便利です!
以下のサイトを参考にやりました!
-http://www.kishiro.com/FreeBSD/get_filename_in_shellscri...
実は簡単にシェルスクリプトでパス文字列からファイル名/デ...
サンプルは以下のとおり。
まず、サンプルのファイルのフルパスを変数に入れます。
# TARGET=/etc/sysconfig/network-scripts/ifcfg-eth0
#
# echo ${TARGET}
/etc/sysconfig/network-scripts/ifcfg-eth0
#
echoすると当然そのまま出ます。
ディレクトリ名を取り出す場合はこんな感じ。
# echo ${TARGET%/*}
/etc/sysconfig/network-scripts
おお!できた!
続いて、ファイル名だけ取り出す場合はこんな感じ。
# echo ${TARGET##*/}
ifcfg-eth0
おお!できた!
***解説 [#m5d278be]
シェルでは変数に対して${'''parameter'''''XXX''}という形式...
とりあえず、備忘のためにmanを貼り付けてみました。
>Parameter Expansion
> The ‘$’ character introduces parameter expansion, ...
> The parameter name or symbol to be expanded may be e...
> serve to protect the variable to be expanded from c...
> be interpreted as part of the name.
>
> When braces are used, the matching ending brace is t...
> within a quoted string, and not within an embedded ...
> parameter expansion.
>
> ${parameter}
> The value of parameter is substituted. The b...
> tional parameter with more than one digit...
> which is not to be interpreted as part of its...
>
> If the first character of parameter is an exclamatio...
> introduced. Bash uses the value of the variable fo...
> the variable; this variable is then expanded and tha...
> tion, rather than the value of parameter itself. Th...
> tions to this are the expansions of ${!prefix*} and ...
> point must immediately follow the left brace in orde...
>
> In each of the cases below, word is subject to tild...
> stitution, and arithmetic expansion. When not perfo...
> parameter that is unset or null; omitting the colon ...
> unset.
>
> ${parameter:-word}
> Use Default Values. If parameter is unset or...
> Otherwise, the value of parameter is substitu...
> ${parameter:=word}
> Assign Default Values. If parameter is unset...
> parameter. The value of parameter is then su...
> parameters may not be assigned to in this way.
> ${parameter:?word}
> Display Error if Null or Unset. If parameter...
> message to that effect if word is not present...
> shell, if it is not interactive, exits. Oth...
> ${parameter:+word}
> Use Alternate Value. If parameter is null or...
> expansion of word is substituted.
> ${parameter:offset}
> ${parameter:offset:length}
> Substring Expansion. Expands to up to length...
> acter specified by offset. If length is omit...
> starting at the character specified by off...
> sions (see ARITHMETIC EVALUATION below). len...
> equal to zero. If offset evaluates to a ...
> offset from the end of the value of parameter...
> positional parameters beginning at offset. ...
> *, the result is the length members of the ar...
> negative offset is taken relative to one g...
> array. Note that a negative offset must be s...
> to avoid being confused with the :- expansion...
> positional parameters are used, in which case...
>
> ${!prefix*}
> ${!prefix@}
> Expands to the names of variables whose names...
> character of the IFS special variable.
>
> ${!name[@]}
> ${!name[*]}
> If name is an array variable, expands to the ...
> If name is not an array, expands to 0 if name...
> and the expansion appears within double quote...
>
> ${#parameter}
> The length in characters of the value of para...
> the value substituted is the number of positi...
> name subscripted by * or @, the value substit...
>
> ${parameter#word}
> ${parameter##word}
> The word is expanded to produce a pattern ...
> matches the beginning of the value of paramet...
> expanded value of parameter with the sho...
> longest matching pattern (the ‘‘##’’ case) de...
> removal operation is applied to each position...
> resultant list. If parameter is an array var...
> removal operation is applied to each member ...
> resultant list.
>
> ${parameter%word}
> ${parameter%%word}
> The word is expanded to produce a pattern jus...
> matches a trailing portion of the expan...
> expansion is the expanded value of parameter ...
> case) or the longest matching pattern (the ...
> the pattern removal operation is applied to e...
> expansion is the resultant list. If parame...
> *, the pattern removal operation is applied t...
> expansion is the resultant list.
>
> ${parameter/pattern/string}
> The pattern is expanded to produce a patter...
> expanded and the longest match of pattern aga...
> Ipattern begins with /, all matches of patter...
> first match is replaced. If pattern begins w...
> expanded value of parameter. If pattern ...
> expanded value of parameter. If string is nu...
> following pattern may be omitted. If para...
> applied to each positional parameter in turn,...
> parameter is an array variable subscript...
> applied to each member of the array in turn, ...
終了行:
*Linux:シェルスクリプトでパス文字列からファイル名/ディ...
これは便利です!
以下のサイトを参考にやりました!
-http://www.kishiro.com/FreeBSD/get_filename_in_shellscri...
実は簡単にシェルスクリプトでパス文字列からファイル名/デ...
サンプルは以下のとおり。
まず、サンプルのファイルのフルパスを変数に入れます。
# TARGET=/etc/sysconfig/network-scripts/ifcfg-eth0
#
# echo ${TARGET}
/etc/sysconfig/network-scripts/ifcfg-eth0
#
echoすると当然そのまま出ます。
ディレクトリ名を取り出す場合はこんな感じ。
# echo ${TARGET%/*}
/etc/sysconfig/network-scripts
おお!できた!
続いて、ファイル名だけ取り出す場合はこんな感じ。
# echo ${TARGET##*/}
ifcfg-eth0
おお!できた!
***解説 [#m5d278be]
シェルでは変数に対して${'''parameter'''''XXX''}という形式...
とりあえず、備忘のためにmanを貼り付けてみました。
>Parameter Expansion
> The ‘$’ character introduces parameter expansion, ...
> The parameter name or symbol to be expanded may be e...
> serve to protect the variable to be expanded from c...
> be interpreted as part of the name.
>
> When braces are used, the matching ending brace is t...
> within a quoted string, and not within an embedded ...
> parameter expansion.
>
> ${parameter}
> The value of parameter is substituted. The b...
> tional parameter with more than one digit...
> which is not to be interpreted as part of its...
>
> If the first character of parameter is an exclamatio...
> introduced. Bash uses the value of the variable fo...
> the variable; this variable is then expanded and tha...
> tion, rather than the value of parameter itself. Th...
> tions to this are the expansions of ${!prefix*} and ...
> point must immediately follow the left brace in orde...
>
> In each of the cases below, word is subject to tild...
> stitution, and arithmetic expansion. When not perfo...
> parameter that is unset or null; omitting the colon ...
> unset.
>
> ${parameter:-word}
> Use Default Values. If parameter is unset or...
> Otherwise, the value of parameter is substitu...
> ${parameter:=word}
> Assign Default Values. If parameter is unset...
> parameter. The value of parameter is then su...
> parameters may not be assigned to in this way.
> ${parameter:?word}
> Display Error if Null or Unset. If parameter...
> message to that effect if word is not present...
> shell, if it is not interactive, exits. Oth...
> ${parameter:+word}
> Use Alternate Value. If parameter is null or...
> expansion of word is substituted.
> ${parameter:offset}
> ${parameter:offset:length}
> Substring Expansion. Expands to up to length...
> acter specified by offset. If length is omit...
> starting at the character specified by off...
> sions (see ARITHMETIC EVALUATION below). len...
> equal to zero. If offset evaluates to a ...
> offset from the end of the value of parameter...
> positional parameters beginning at offset. ...
> *, the result is the length members of the ar...
> negative offset is taken relative to one g...
> array. Note that a negative offset must be s...
> to avoid being confused with the :- expansion...
> positional parameters are used, in which case...
>
> ${!prefix*}
> ${!prefix@}
> Expands to the names of variables whose names...
> character of the IFS special variable.
>
> ${!name[@]}
> ${!name[*]}
> If name is an array variable, expands to the ...
> If name is not an array, expands to 0 if name...
> and the expansion appears within double quote...
>
> ${#parameter}
> The length in characters of the value of para...
> the value substituted is the number of positi...
> name subscripted by * or @, the value substit...
>
> ${parameter#word}
> ${parameter##word}
> The word is expanded to produce a pattern ...
> matches the beginning of the value of paramet...
> expanded value of parameter with the sho...
> longest matching pattern (the ‘‘##’’ case) de...
> removal operation is applied to each position...
> resultant list. If parameter is an array var...
> removal operation is applied to each member ...
> resultant list.
>
> ${parameter%word}
> ${parameter%%word}
> The word is expanded to produce a pattern jus...
> matches a trailing portion of the expan...
> expansion is the expanded value of parameter ...
> case) or the longest matching pattern (the ...
> the pattern removal operation is applied to e...
> expansion is the resultant list. If parame...
> *, the pattern removal operation is applied t...
> expansion is the resultant list.
>
> ${parameter/pattern/string}
> The pattern is expanded to produce a patter...
> expanded and the longest match of pattern aga...
> Ipattern begins with /, all matches of patter...
> first match is replaced. If pattern begins w...
> expanded value of parameter. If pattern ...
> expanded value of parameter. If string is nu...
> following pattern may be omitted. If para...
> applied to each positional parameter in turn,...
> parameter is an array variable subscript...
> applied to each member of the array in turn, ...
ページ名: