0

Want to verify if site domain contains "com". Assume I have shell varibale as

export FIRST_URL="http://www.11111.com" 
export SECOND_URL="http://www.22222.org"

User calls Python script with parameter (partial shell varibale) as

python2.7  FIRST     # OR
python2.7  SECOND 

Python script is,

import sys, os, subprocess

PART_URL = sys.argv[1]
print( "PART_URL=",PART_URL)

COMPLETE_URL = PART_URL+'_URL'       # Formed a full shell varibale
cmd_str='echo {} | grep \"com\".format(COMPLETE_URL)'  # equivalent to echo $FIRST_URL | grep com

my_site=subprocess.check_output(cmd_str, shell=True)   # Note we cant use subprocess.run() in Python Python 2.7
print("The validated Site is ", my_site)

The output should be "The validated Site is http://www.11111.com"

Krishna
  • 501
  • 1
  • 8
  • 17
  • Why don't you just pass the URL itself as the argument? And why are you shelling out to `grep` to check the format rather than doing the validation in the script itself? – chepner Jan 07 '20 at 20:59

2 Answers2

0

Refer How to access environment variable values?

$ export FIRST_VAR="http://www.11111.com"
$ python
>>> foo = 'FIRST'
>>> print os.environ[foo + '_VAR']
http://www.11111.com
>>>
Sharad
  • 9,282
  • 3
  • 19
  • 36
  • Thanks for responding but you haven't read completely the query, 1) One part is forming the dynamic environment string, 2) second part is get the value for that created environment. – Krishna Jan 05 '20 at 22:09
  • I did read it :) May be I should have added more clarification. 1. Dynamic variable name: I haven't posted the full code snippet above. I've hardcoded `foo` to the value `FIRST` but that's something you can read from sys.argv[1], add a pre-defined suffix (in this case `_VAR`) and get the full variable name. 2. Retrieving the value: os.environ[foo + '_VAR'] will get the value for that created environment variable. – Sharad Jan 06 '20 at 07:42
  • Thanks, i wanted to see the code line with subprocess.check_output(cmd_str, shell=True) , what is cmd_str? could you post that please? – Krishna Jan 06 '20 at 13:33
  • os.environ directly gives you access to the value of the environment variables. Why do you want to launch another process, run echo and get the output when there's a much simpler way to do it? – Sharad Jan 06 '20 at 14:45
  • I edited script as above to validate the site if contains 'com' in it. Could you see it please. – Krishna Jan 06 '20 at 22:59
  • First export those 2 varibles with values, they run python script as "python2.7 test.py FISRT" or "python2.7 test.py SECOND" – Krishna Jan 06 '20 at 23:03
  • The domain may contain more than 1 period. foo.bar.bat or foo.bar.bat.co - I think the domain detection logic could/should be more generic to handle more than just .com. Also, regular expressions (python module re) would be a much better choice than using grep to detect and filter out substrings. Please note StackOverflow is a forum to get a resolution to queries and not to get readymade answers. I'd recommend reading on the `os` and `re` modules and taking a python tutorial to code your logic in a more efficient way (rather than force-fitting shell constructs in a python script). – Sharad Jan 08 '20 at 04:46
0

Figured it out,

COMPLETE_URL = PART_URL+'_URL'     
cmd_str='echo ${} | grep \'com\'.format(COMPLETE_URL)' 
my_site=subprocess.check_output(cmd_str, shell=True)  

Alteratively, we can use,

my_site=subprocess.call(shlex.split(cmd_str))
Krishna
  • 501
  • 1
  • 8
  • 17