Using CmsShell on Ubuntu systems

CmsShell is a neat tool to access the OpenCms VFS from the command line. This can be extremely useful when your system can’t be accessed anymore using the web interface or when performing batch updates. The script can be found in the OpenCms webapp at WEB-INF/cmsshell.sh.
Unfortunately the script that ships with the OpenCms webapp doesn’t work on Ubuntu systems as it is. These are the steps to make it run.
First be sure that it’s executable: chmod +x cmsshell.sh
When trying to run it using ./cmsshell.sh you will very likely see an error:

bash: ./cmsshell.sh: /bin/sh^M: bad interpreter: No such file or directory

This is caused by dos line breaks that are included in the file. A tool to remove those can be found in the package tofrodos (used to be in sysutils):
sudo apt-get install tofrodos
Run it on the file by issuing
dos2unix cmsshell.sh
Try to run the file again: ./cmsshell.sh, which will result in another error:

/cmsshell.sh: 8: pushd: not found
./cmsshell.sh: 9: dirs: not found
./cmsshell.sh: 10: popd: not found
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencms/main/CmsShell
Caused by: java.lang.ClassNotFoundException: org.opencms.main.CmsShell
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: org.opencms.main.CmsShell.  Program will exit.

To make it work you need to open the script and change the line
OPENCMS_BASE=`dirs +0`
to
OPENCMS_BASE=`pwd`
When saving the change and executing the script you should see OpenCms start and end at the prompt where you can login and execute any useful action you can think of.