Use this script file to su into the cpanel user defined by the path.

#!/bin/bash
THISUSER=`pwd | cut -d'/' -f 3`
echo
echo now running as $THISUSER;
echo
su $THISUSER -s /bin/bash

This is a easy option if you need to work on files as the correct owner.

For example, if the path is /home/username/this/file path, you will su as username.

Installation

cat > ~/suthis.sh <<- EOM
#!/bin/bash
THISUSER=\`pwd | cut -d'/' -f 3\`
echo
echo now running as \$THISUSER;
echo
su \$THISUSER -s /bin/bash
EOM
chmod +x ~/suthis.sh

Operation

Now you can run it by typing

~/suthis.sh

Extended version

This version takes several extra optional parameters and will run them against using user determined user. If no parameters, then open a shell with that user.

Installation

Download and install this script by running the following

cat > ~/suthis.sh <<- EOM
#!/bin/bash
THISUSER=\`pwd | cut -d'/' -f 3\`
echo
if [[ \$* ]]; then
   echo Running commands as \$THISUSER
   echo
   for i in \`seq 1 \$#\`
   do
      eval a=\\\$\$i
      echo "Run: '\$a'"
      echo
      su \$THISUSER -s /bin/bash -c "\$a"
      echo
   done
else
   echo Now running as \$THISUSER
   echo
   su \$THISUSER -s /bin/bash
fi
EOM
chmod +x ~/suthis.sh

Operation

You can open a shell as above. Or run a command by passing it as an optional parameter. Use double quotes for commands with spaces.

~/suthis.sh "ls -l"

You can have as many commands as you like.

~/suthis.sh "ls -l" ls "ls" "echo hello"