I want to take first 4 letters from string1 (in this case: qwer), compare it to string2 (in this case qwerWhatever1) and if string2 has in it string1, remove string1 from string2 and put it to string3, so string3 would be: Whatever1. And after this I want to compare if $currdir has $string3.
To take first four letters I can do:
echo $string1 | awk '{print substr($1,1,4) }'
but I am not sure what to do else.
Maybe somebody can help me how this can be established.
if your characters are same. what i meant was if the first four characters are always same then you can do something like this:-
lets just say the first four characters are =abcd
export string1=abcdWhatever
export string2=abcdWhatever1
export currdir=`pwd`
echo $currdir /dir/dir/Whatever1
if [[ `grep "abcd" string1` ]]
then
if [[ `grep "abcd" string2` ]]
then
cat string2 | grep -v "abcd" string2 > string3
fi
fi
if [[ $curdir == string3 ]]
then
echo "string 3 is equal to current directory"
fi
$ cat match.sh
#!/usr/local/bin/bash
string1=qwerWhatever
string2=qwerWhatever1
currdir=/dir/dir/Whatever1
s1f4=$(echo $string1 | cut -c1-4)
if [[ "$string2" =~ $s1f4 ]]; then
echo $s1f4 found in string2
strng3=$(echo $string2 | sed "s/\(.*\)$s1f4\(.*\)/\1\2/")
if [[ "$currdir" =~ "$string3" ]]; then
echo Current dir has string3
fi
else
echo Intial match failed
fi
$
$
$
$
$ ./match.sh
qwer found in string2
Current dir has string3
$
That is bash's built-in comparison operator for regular expressions.
Introduced in bash version 3.
that will work only in bash shell. if you want to stick with the ksh, you probably need to use external commands grep/sed.