rshetye
rshetye

Reputation: 667

shell script to process a folder path

I am unable to process the following shell script

#!/bin/sh
P= '/mnt/temp/'
echo $P
Q= `echo $P` | sed -e "s/^.*\(.\)$/\1/"
echo 'Q is' $Q
echo ${P%?}

I expect the output as

/mnt/temp/
Q is /
/mnt/tmp

[edit]

As a next step I want to update remove the trailing / in P so am trying

if  ${Q} ="/"
then
   echo in
   P=${P%?}
fi

but on execution it says

/: Permission denied

Upvotes: 0

Views: 97

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185760

Spaces are not allowed in variable assignation in , so :

#!/bin/sh
P='/mnt/temp/'
echo "$P"
Q=$(echo "$P" | sed -e "s/^.*\(.\)$/\1/")
echo "Q is $Q"
echo "${P%?}"

And use more quotes, see http://mywiki.wooledge.org/Quotes http://mywiki.wooledge.org/Arguments and http://wiki.bash-hackers.org/syntax/words

Upvotes: 5

Related Questions