Number output of shell scripts during the interview

Source: Internet
Author: User

Question 1: Get sequential numbers from 1 to 9
Shell Method 1:

For I in 'seq 1 9'; do echo $ I; doneSimilar to seq 1 9 or seq 9

123456789

Shell Method 2:

For (I = 1; I <= 9; I ++); do echo $ I; doneSimilar to for I in {1 .. 9}; do echo $ I; done

123456789

Python method 3:

Script content

#!/usr/bin/pythonfor i in range(1,10):print ('%s') % i

Result output

123456789

Sehll Method 4:

Seq-s ""-w 9

1 2 3 4 5 6 7 8 9

Shell Method 5:

Echo {1 .. 9}

1 2 3 4 5 6 7 8 9

Question 2: Get the following digital output
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Shell method:

#!/bin/basha=({1..5})for i in {5..1}doecho ${a[*]:0:$i}done

Question 3: Get the following digital output
55555
4444
333
22
1

Shell method:

#!/bin/bashfor ((i=5; i>=1; i-- ))dofor ((j=1; j<=i; j++ ))doecho -n "$i"doneecho ""done

Question 4: Output 26 27 28 29 30 31 32 33 34 35

Echo 2 {6 .. 9} 3 {0 .. 5}

26 27 28 29 30 31 32 33 34 35

Question 5: Output numbers in descending order 9-1

Seq-s ""-w 9-1 1

9 8 7 6 5 4 3 2 1

Question 6: an odd number between 0 and 36 is output.

Seq-s "" 1 2 36

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35

Question 7: an even number between 0 and 36 is output.

Seq-s "" 0 2 36

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36

Question 8: Output 0-100 in three digits

Method 1: printf "% 03d" {0. 100}; echo

000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

Method 2: echo {000... 100}

000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

Method 3: seq-s ""-w 0 100

000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

Method 4: awk 'in in {for (I = 0; I <= 100; I ++) printf ("% 03d", I )}'

000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

Method 5: for (I = 0; I <= 100; I ++); do printf "% 03d" $ I; done

000 001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

If only 1-100 is output, you can also use

Yes | grep y | sed 100q | awk '{printf ("% 03d", NR)}'; echoVertical output: yes | nl-ba | tr ''0 | sed 100q | cut-B 4-6

001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100

You are welcome to leave a message for me! I am very grateful for providing different solutions and problems!

This article is from the "Old Xu's Private food" blog and will not be reposted!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.