我的尝试是使用一个栈文件:
#!/bin/sh
echo "First entry" > stack.txt
echo "More info for the first entry" >> stack.txt
echo "Even more info for the first entry" >> stack.txt
while read ENTRY < stack.txt; do
INFO2=`tail -n +2 stack.txt | head -n 1`
INFO3=`tail -n +3 stack.txt | head -n 1`
tail -n "+4" stack.txt > stack2.txt
# Process the entry...
# When we have to push something:
echo "New entry" > stack.txt
echo "Info 2" >> stack.txt
echo "Info 3" >> stack.txt
# Finally, rebuild stack
cat stack2.txt >> stack.txt
done
这个方法完全可行,但感觉不太对。有没有更少“hacky”的方法呢?
非常感谢您的帮助!
sed -n 2p
要比将尾部输出再通过管道传递给头部更加简洁。 - William Pursell