How to test whether PATH has a specific directory in it in Bash on Linux?

Posted on

How to test whether PATH has a specific directory in it in Bash on Linux? For example, how to test whether /usr/local/mytool/bin is in $PATH? Directories in the $PATH variable are separated by :. So you can use a regular expression to test whether the $PATH contains a directory. The code for your specific example
Read more

How to test a file or directory exists in Python?

Posted on

In Bash, the [ -f ] and [ -d ] tests can test whether a file or a directory exist. What are the corresponding python method for these tests? For -f: **os.path.isfile(path)** Return True if path is an existing regular file. This follows symbolic links. For -d: **os.path.isdir(path)** Return True if path is an existing
Read more

How to get file extension in JavaScript?

Posted on

How to get file extension from a file name in JavaScript? For examples, For “file.txt”, I want to get “txt”. For “file2.multi.ext.fileext”, I want to get “fileext”. This JavaScript function works well for me. function getExt(filename) { var idx = filename.lastIndexOf(‘.’); // handle cases like, .htaccess, filename return (idx < 1) ? “” : filename.substr(idx
Read more

How to get a user’s home directory from the username in a variables in Bash?

Posted on

In Bash, ~username gives the username user’s home. However, this does not work for situations where username is in a variable such as ~$user How to get the home directory from the user name in a variable in Bash? You can use this Bash script snippet: userhome=$(eval echo ~$user) Here, $user get evaluated to its
Read more

In Vim, how to search and replace in visual selection?

Posted on

Search and replace in the whole text in Vim can be done by :%s/from/to/gc. But how to search and replace in visual selection only? The %V atom in Vim restricts a pattern so that it matches only inside the visual selection. So, your command for searching and replacing only in the visual selection can be
Read more

How do I force Linux to unmount a filesystem?

Posted on

Some time, Linux fails to unmount a filesystem reporting “device is busy”. I understand that this helps avoid data lost by disallowing unmouting a filesystem when it is being used. But for some situations, I am sure there is something wrong happened or I care not data lost, such as a NFS mounting while the
Read more

Linux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine

Posted on

Linux boots failed with “sulogin: can not open password database” while the /etc/passwd and /etc/shadow files look fine. How to fix this? The OS is Fedora 22. You may try this trick Step 1: boot Linux with rw init=/bin/bash following this tutorial. Step 2: after Linux booted, disable SELinux following this tutorial. Reboot. If it
Read more

How to debug media print view of Web page in Firefox?

Posted on

How to debug the media print view set by @media print {} in CSS of Web pages in Firefox? In firefox, after opening the Web page, First, hit “Shift + F2” to open the “Developer Toolbar” at the bottom. Second, in the “Developer Toobar”, input media emulate print and Firefox will show the print view
Read more