我正在尝试在bash中实现REPL (read-eval-print loop)。如果这样的事情已经存在,请忽略以下内容并用指针回答这个问题。
我们以此脚本为例(将其命名为test.sh
):
if true
then
echo a
else
echo b
fi
echo c
我想要做的是逐行阅读这个脚本,检查我到目前为止所阅读的内容是否是一个完整的bash表达式;如果完整,eval
它;否则继续阅读下一行。下面的脚本有希望地说明了我的想法(虽然它不太有效)。
x=""
while read -r line
do
x=$x$'\n'$line # concatenate by \n
# the line below is certainly a bad way to go
if eval $x 2>/dev/null; then
eval $x # code seems to be working, so eval it
x="" # empty x, and start collecting code again
else
echo 'incomplete expression'
fi
done < test.sh
对于bash脚本,我想将其解析为语法上完整的表达式,计算每个表达式,捕获输出,最后标记源代码和输出(例如,使用Markdown / HTML / LaTeX / ...)。例如,对于脚本
echo a
echo b
我想要实现的是这样的输出:
```bash
echo a
```
```
a
```
```bash
echo b
```
```
b
```
而不是评估整个脚本并捕获所有输出:
```bash
echo a
echo b
```
```
a
b
```
答案 0 :(得分:6)
bash -n -c "$command_text"
...将确定您的$command_text
是否是一个语法上有效的脚本而不实际执行它。
请注意,#34;语法上有效&#34;之间有很大的空间。并且&#34;纠正&#34;。如果要正确解析语言,请考虑采用类似http://shellcheck.net/的内容。
答案 1 :(得分:4)
以下脚本应生成您期望的Markdown输出。
eval "set -n; $x"
用于通过检查命令中的语法错误来验证命令是否完整。只有没有语法错误的命令才会被视为完成,执行并显示在输出Markdown中。
请注意,要处理的输入脚本在子shell中执行,因此不会干扰处理脚本本身(即输入脚本可以使用与处理脚本相同的变量名称,并且不能更改处理脚本中的变量值)。唯一的例外是名为___internal__variable___
的特殊变量。
如何实现这一目标有两种方法,我将在下面介绍。在版本1 中,每当处理新的完整命令时,执行它之前的所有语句以创建&#34; context&#34;为命令。这有效地多次运行输入脚本。
在版本2 中,在执行每个完整命令后,子shell的环境存储在变量中。然后,在执行下一个命令之前,在子shell中恢复先前的环境。
#!/bin/bash
x="" # Current
y="" # Context
while IFS= read -r line # Keep indentation
do
[ -z "$line" ] && continue # Skip empty lines
x=$x$'\n'$line # Build a complete command
# Check current command for syntax errors
if (eval "set -n; $x" 2> /dev/null)
then
# Run the input script up to the current command
# Run context first and ignore the output
___internal_variable___="$x"
out=$(eval "$y" &>/dev/null; eval "$___internal_variable___")
# Generate command markdown
echo "=================="
echo
echo "\`\`\`bash$x"
echo "\`\`\`"
echo
# Generate output markdown
if [ -n "$out" ]
then
echo "Output:"
echo
echo "\`\`\`"
echo "$out"
echo "\`\`\`"
echo
fi
y=$y$'\n'$line # Build context
x="" # Clear command
fi
done < input.sh
#!/bin/bash
x="" # Current command
y="true" # Saved environment
while IFS= read -r line # Keep indentation
do
[ -z "$line" ] && continue # Skip empty lines
x=$x$'\n'$line # Build a complete command
# Check current command for syntax errors
if (eval "set -n; $x" 2> /dev/null)
then
# Run the current command in the previously saved environment
# Then store the output of the command as well as the new environment
___internal_variable_1___="$x" # The current command
___internal_variable_2___="$y" # Previously saved environment
out=$(bash -c "${___internal_variable_2___}; printf '<<<BEGIN>>>'; ${___internal_variable_1___}; printf '<<<END>>>'; declare -p" 2>&1)
# Separate the environment description from the command output
y="${out#*<<<END>>>}"
out="${out%%<<<END>>>*}"
out="${out#*<<<BEGIN>>>}"
# Generate command markdown
echo "=================="
echo
echo "\`\`\`bash$x"
echo "\`\`\`"
echo
# Generate output markdown
if [ -n "$out" ]
then
echo "Output:"
echo
echo "\`\`\`"
echo "$out"
echo "\`\`\`"
echo
fi
x="" # Clear command
fi
done < input.sh
对于输入脚本input.sh
:
x=10
echo "$x"
y=$(($x+1))
echo "$y"
while [ "$y" -gt "0" ]
do
echo $y
y=$(($y-1))
done
输出将是:
==================
```bash
x=10
```
==================
```bash
echo "$x"
```
Output:
```
10
```
==================
```bash
y=$(($x+1))
```
==================
```bash
echo "$y"
```
Output:
```
11
```
==================
```bash
while [ "$y" -gt "0" ]
do
echo $y
y=$(($y-1))
done
```
Output:
```
11
10
9
8
7
6
5
4
3
2
1
```
答案 2 :(得分:3)
假设您的测试命令存储在名为“example”的文件中。也就是说,使用与之前答案相同的命令:
$ cat example
x=3
echo "$x"
y=$(($x+1))
echo "$y"
while [ "$y" -gt "0" ]
do
echo $y
y=$(($y-1))
done
命令:
$ (echo 'PS1=; PROMPT_COMMAND="echo -n =====; echo"'; cat example2 ) | bash -i
产生
=====
x=3
=====
echo "$x"
3
=====
y=$(($x+1))
=====
echo "$y"
4
=====
=====
=====
while [ "$y" -gt "0" ]
> do
> echo $y
> y=$(($y-1))
> done
4
3
2
1
=====
exit
如果您对循环的中间结果感兴趣,请执行以下命令:
$ ( echo 'trap '"'"'echo; echo command: $BASH_COMMAND; echo answer:'"'"' DEBUG'; cat example ) | bash
结果:
command: x=3
answer:
command: echo "$x"
answer:
3
command: y=$(($x+1))
answer:
command: echo "$y"
answer:
4
command: [ "$y" -gt "0" ]
answer:
command: echo $y
answer:
4
command: y=$(($y-1))
answer:
command: [ "$y" -gt "0" ]
answer:
command: echo $y
answer:
3
command: y=$(($y-1))
answer:
command: [ "$y" -gt "0" ]
answer:
command: echo $y
answer:
2
command: y=$(($y-1))
answer:
command: [ "$y" -gt "0" ]
answer:
command: echo $y
answer:
1
command: y=$(($y-1))
answer:
command: [ "$y" -gt "0" ]
answer:
附录1
将以前的结果更改为其他格式并不困难。例如,这个小的perl脚本:
$ cat formatter.pl
#!/usr/bin/perl
#
$state=4; # 0: answer, 1: first line command, 2: more command, 4: unknown
while(<>) {
# print $state;
if( /^===COMMAND===/ ) {
print "===\n";
$state=1;
next;
}
if( $state == 1 ) {
print;
$state=2;
next;
}
if( $state == 2 && /^>+ (.*)/ ) {
print "$1\n";
next;
}
if( $state == 2 ) {
print "---\n";
$state=0;
redo;
}
if( $state == 0 ) {
print;
next;
}
}
在命令中使用时:
( echo 'PS1="===COMMAND===\n"'; cat example ) | bash -i 2>&1 | ./formatter.pl
给出了这个结果:
===
x=3
===
echo "$x"
---
3
===
y=$(($x+1))
===
echo "$y"
---
4
===
===
===
while [ "$y" -gt "0" ]
do
echo $y
y=$(($y-1))
done
---
4
3
2
1
===
exit
答案 3 :(得分:-1)
代替pidfiles,只要您的脚本具有唯一可识别的名称,您就可以执行以下操作:
#!/bin/bash
COMMAND=$0
# exit if I am already running
RUNNING=`ps --no-headers -C${COMMAND} | wc -l`
if [ ${RUNNING} -gt 1 ]; then
echo "Previous ${COMMAND} is still running."
exit 1
fi
... rest of script ...