How to get pid given the process name

I think it is easier to use pgrep $ pgrep bluetoothd 441 Otherwise, you can use awk: ps -ef | awk ‘$8==”name_of_process” {print $2}’ For example, if ps -efhas a line like: root 441 1 0 10:02 ? 00:00:00 /usr/sbin/bluetoothd Then ps -ef | awk ‘$8==”/usr/sbin/bluetoothd” {print $2}’ returns 441. In ksh pgrep is not … Read more

How can I find a file/directory that could be anywhere on linux command line? [closed]

“Unfortunately this seems to only check the current directory, not the entire folder”. Presumably you mean it doesn’t look in subdirectories. To fix this, use find -name “filename” If the file in question is not in the current working directory, you can search your entire machine via find / -name “filename” This also works with … Read more

Select unique or distinct values from a list in UNIX shell script

You might want to look at the uniq and sort applications. ./yourscript.ksh | sort | uniq (FYI, yes, the sort is necessary in this command line, uniq only strips duplicate lines that are immediately after each other) EDIT: Contrary to what has been posted by Aaron Digulla in relation to uniq‘s commandline options: Given the … Read more

How to detect if a script is being sourced

Robust solutions for bash, ksh, zsh, including a cross-shell one, plus a reasonably robust POSIX-compliant solution: Version numbers given are the ones on which functionality was verified – likely, these solutions work on much earlier versions, too – feedback welcome. Using POSIX features only (such as in dash, which acts as /bin/sh on Ubuntu), there … Read more

What is the difference between $(command) and `command` in shell programming?

The backticks/gravemarks have been deprecated in favor of $() for command substitution because $() can easily nest within itself as in $(echo foo$(echo bar)). There are other differences such as how backslashes are parsed in the backtick/gravemark version, etc. See BashFAQ/082 for several reasons to always prefer the $(…) syntax. Also see the POSIX spec … Read more

How to mkdir only if a directory does not already exist?

Try mkdir -p: mkdir -p foo Note that this will also create any intermediate directories that don’t exist; for instance, mkdir -p foo/bar/baz will create directories foo, foo/bar, and foo/bar/baz if they don’t exist. Some implementation like GNU mkdir include mkdir –parents as a more readable alias, but this is not specified in POSIX/Single Unix … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)