script to check bash scripts - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: script to check bash scripts (/thread-21169.html) |
script to check bash scripts - Skaperen - Sep-18-2019 unlike python, bash does not tell you where you left out a quote or { or }. it just tells you at the end that it hit EOF looking for a matching character. this script (for 3.6 or later) is a quickie i wrote to find just such a problem in a huge bash script i have (i have about 10 of these huge ones that i wish i had time to rewrite into python). yes, i do tend to code like this (one letter variables, etc) for quickies. do not use this as an example of good coding. #!/usr/bin/env python3 # -*- coding: utf-8 -*- import sys n = 0 z = "'"+'"{' for l in sys.stdin: n += 1 l = l.strip() if l and l[0]=='#': continue for q in z: if q=='{': if l and l[-1]=='}': continue for x in range(len(l)): s=' '*x l = l.replace('()'+s+'{','()'+s) l = l.replace('}','{') s = l.split(q) if len(s)%2 == 0: print(f'oddetall av {q} i linje {n} ... {l}') RE: script to check bash scripts - DeaD_EyE - Sep-18-2019 If you do it for your own, everything is allowed! For example, sometimes I just paste some text into triple quotes, to work with it in Python. This sounds very silly, but I know I am not the only one who is doing this. So, it has solved you problem. RE: script to check bash scripts - buran - Sep-18-2019 Just to mention that l is as str , thusif l and l[0]=='#': is better just if l.startswith("#"): and if l and l[-1]=='}': is better as if l.endswith("}"): this way you avoid having to check it's not an empty string RE: script to check bash scripts - Skaperen - Sep-19-2019 as long as those methods work ok when the string is empty, great. i just don't remember all those fine details until i've used it a few times. thanks for the suggestion. |