【shell】用什么命令实现 输出一个很长的字符串 每80字符自动换行
4个回答
展开全部
我和楼上对你这个题目的理解不同,这就势必造成答案的不同。
根据我的理解,答案如下(也是用shell脚本来实现):
#!/bin/bash
str="aaaaaaaabbbbbbbbbbbbbccccccccccccccccddddddddddddddeeeeeeeeeeeeeeeeefffffffffffffffffffggggggggggggggggghhhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkklllllllllllllllllllllllllllmmmmmmmmmmmmmmmnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqqqqqrrrrrrrrrrrrrrsssssssssssssttttttttttttuuuuuuuuuuuuuuvvvvvvvvvvvvyyyyyyyyyyxxxxxxxxxxyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzz" # a long long string
len=${#str} # get string length
# calculate the total line for print
strtail=$((len%80))
line=$((len/80)) # not count in tail if have
sPos=0 # initialize the start position
for ((n=1;n<=$line;n++))
do
echo ${str:sPos:80}
sPos=$((sPos+80))
done
[ $strtail -ne 0 ] && echo ${str:sPos:strtail}
根据我的理解,答案如下(也是用shell脚本来实现):
#!/bin/bash
str="aaaaaaaabbbbbbbbbbbbbccccccccccccccccddddddddddddddeeeeeeeeeeeeeeeeefffffffffffffffffffggggggggggggggggghhhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjkkkkkkkkkkkkkkkkkklllllllllllllllllllllllllllmmmmmmmmmmmmmmmnnnnnnnnnnnoooooooooooopppppppppppppppppqqqqqqqqqqqqqqqqrrrrrrrrrrrrrrsssssssssssssttttttttttttuuuuuuuuuuuuuuvvvvvvvvvvvvyyyyyyyyyyxxxxxxxxxxyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzz" # a long long string
len=${#str} # get string length
# calculate the total line for print
strtail=$((len%80))
line=$((len/80)) # not count in tail if have
sPos=0 # initialize the start position
for ((n=1;n<=$line;n++))
do
echo ${str:sPos:80}
sPos=$((sPos+80))
done
[ $strtail -ne 0 ] && echo ${str:sPos:strtail}
展开全部
编辑一个脚本:disp80
#!/bin/sh
chars=`echo $1|wc -c`
lines=`expr $chars / 80`
i=0
while [ $i -le $lines ]
do
nstart=`expr 80 \* $i + 1`
nend=`expr 80 \* $i + 80`
echo $1|cut -c ${nstart}-${nend}
i=`expr $i + 1`
done
在命令行下执行disp80 <字符串>即可。
#!/bin/sh
chars=`echo $1|wc -c`
lines=`expr $chars / 80`
i=0
while [ $i -le $lines ]
do
nstart=`expr 80 \* $i + 1`
nend=`expr 80 \* $i + 80`
echo $1|cut -c ${nstart}-${nend}
i=`expr $i + 1`
done
在命令行下执行disp80 <字符串>即可。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-09-23
展开全部
#include "stdio.h"
void main()
{
extern enter_string(char str[80]);//顺便学学调用外部函数
extern delete_string(char str[],char ch);
extern print_string(char str[]);
char c;
char str[80];
enter_string(str);
scanf("%c",&c);//输入要删除的字符
delete_string(str,c);
print_string(str);
}
enter_string (char str[80])//外部函数,读入字符串
{
gets(str);
}
delete_string(char str[],char ch)
{
int i,j;
for(i=j=0;str[i]!='\0';i++)
if(str[i]!=ch)
str[j++]=str[i];
str[j]='\0';
}
print_string(char str[])
{
printf("%s\n",str);
}
void main()
{
extern enter_string(char str[80]);//顺便学学调用外部函数
extern delete_string(char str[],char ch);
extern print_string(char str[]);
char c;
char str[80];
enter_string(str);
scanf("%c",&c);//输入要删除的字符
delete_string(str,c);
print_string(str);
}
enter_string (char str[80])//外部函数,读入字符串
{
gets(str);
}
delete_string(char str[],char ch)
{
int i,j;
for(i=j=0;str[i]!='\0';i++)
if(str[i]!=ch)
str[j++]=str[i];
str[j]='\0';
}
print_string(char str[])
{
printf("%s\n",str);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CTRL+C 停止
#!/bin/bash
while true
do
for ((i=1;i<=80;i++));do
sleep 1
printf "a"
done
printf "\n"
done
#!/bin/bash
while true
do
for ((i=1;i<=80;i++));do
sleep 1
printf "a"
done
printf "\n"
done
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询