Python Forum
script to check bash scripts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
script to check bash scripts
#1
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}')
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A bash script that is a hack to enable very easy Python Imports vedant13 2 3,077 Aug-04-2018, 06:10 AM
Last Post: wavic
  bash to python: keep.py Skaperen 0 2,772 Apr-08-2017, 07:27 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020