Friday, November 22, 2013

Call Python Within Shell Script

Shell scripts are easier to write but they lack the features provided by a programming language like python. But instead python can be used within shell scripts. Here are two examples.

​1. To get the file name ‘File’ from the full path ‘Path’:
File=`python -c "print '$Path'.split('/')[-1]"`

2. To get the directory ‘Dir’ from the full path ‘Path’ of file ‘File’:
Dir=`python -c "print '$Path'.rstrip(str('$File'))"`