Python Forum

Full Version: script to check bash scripts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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}')
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.
Just to mention that l is as str, thus

if 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
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.