idank / bashlex

Python parser for bash
GNU General Public License v3.0
550 stars 94 forks source link

How to check if a line is a complete bash command or not? #95

Open nhtha opened 3 months ago

nhtha commented 3 months ago

Dear experts, How to use bashlex to separate a bash script file into groups of multi-line commands to execute each group with paramiko.exec_command function? For example, I want to separate the following bash script into 2 groups (multi-line string), first is "data_mp_info=..." command and second is "if .. fi" command: data_mp_info=$(df -BG | awk '$6 == "/data"') if [ "$data_mp_info" != "" ]; then data_mp_size_in_GB=$(echo $data_mp_info | awk '{print $2}' | sed 's/.$//') data_mp_used_in_GB=$(echo $data_mp_info | awk '{print $3}' | sed 's/.$//') data_mp_free_in_GB=$(echo $data_mp_info | awk '{print $4}' | sed 's/.$//') data_mp_used_percent=$(echo $data_mp_info | awk '{print $5}' | sed 's/.$//') else data_mp_size_in_GB=-1 data_mp_used_in_GB=-1 data_mp_free_in_GB=-1 data_mp_used_percent=-1 fi

Please advise, Thank you, Ha Nguyen

nhtha commented 3 months ago

How to check if a command requires another command, for example the rest commands use variable from the first command?