I’ve got two methods to get strings or substrings within current Linux shell command-line. For instance, sometimes we want to get the file full path when source it in shell. Here are my ways to do so.
- Use command of lsof
1
2
3set sourced_file = `/usr/sbin/lsof -p && -w | grep -oE "\s+\/.*\Sourced_Filename"`
set relative_path = `dirname $sourced_file`
set absolute_path = `chdir $relative_path && pwd` - Use command of history
1
2
3set sourced_file = `history 1 | sed -e 's/^.*source\ //g'`
set relative_path = `dirname $sourced_file`
set absolute_path = `chdir $relative_path && pwd`