【趣味shell&python】倒三角控制台输出图形

通过脚本实现如下效果

【趣味shell&python】倒三角控制台输出图形

1,python实现

#!/usr/bin/python
# filename:demo.py
# -*- coding: UTF-8 -*-

if __name__ == '__main__':
    n = 20
    for i in range(1, n):
        m = i * 2 - 1
        char1 = " "
        char2 = "*"
        print char1 * (n-i), char2*m



2,shell实现一

#!/bin/sh

n=20
i=0
char1=" "
char2="*"

while [ $i -lt $n ];do
	m=$(($i*2-1))
	j=$(($n-$i))
	while [ $j -gt 0 ]; do
		echo -n "$char1"
		j=$((j-1))
	done
	while [ $m -gt 0 ]; do
		echo -n "$char2"
		m=$((m-1))
	done
	echo ""
	i=$((i+1))
done

exit 0



3,shell实现二

#!/bin/bash
for i in $(seq 1 $1)
do
	for j in $(seq 1 $(($1-$i)))
	do
		echo -n " "
	done
	for m in $(seq 1 $((2*$i-1)))
	do
		echo -n "*"
	done
	echo
done
exit 0



4,shell实现三

#!/bin/bash

for i in $(seq 1 20)
do
	for x in $(seq 1 $((5-$i)) )
	do
		echo -n " "
	done
	for y in $(seq 1 $(($i*2-1 )) )
	do
		echo -n "*"
	done
	echo
done

exit 0

 

你可能感兴趣的:(python,脚本,bash)