I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.
if [[ $line =~ "$PWD"$ ]] ;then
I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.
if [[ $line =~ "$PWD"$ ]] ;then
In any POSIX-compatible shell you can do:
case $line in (*"$PWD"*)
# whatever your then block had
;;esac
This works in bash, dash, and just about any other shell you can name.
It can also be used to handle multiple possibilities easily. For example:
case $line in
(*"$PWD"*)
echo \$PWD match\!
;;
(*"$OLDPWD"*)
echo \$OLDPWD match\!
;;
(*)
! echo no match\!
;;esac
You can also use alternation:
case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac
Note the use of quoting above:
case $line ...
case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.(*"$PWD"*)
$IFS or filename generation - an unquoted expansion will neither split nor glob.$PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.Yes, recent versions of bash can do this:
$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes
The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.
Note that your regex is checking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:
$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes