Showing posts with label unix commands. Show all posts
Showing posts with label unix commands. Show all posts

Tuesday, 9 February 2021

Shell Script that accepts a positive integer and returns comma separated list of integers in descending order

Description: 
Write a  shell script that accepts exactly 1 argument which must be a positive integer. 
The script will print a comma-separated list of integers, all on the same line in descending order

Script: decreaseNum.sh

 

#!/bin/bash
#-----------------------------------------------
#user input
num=$1
temp=''
         
        #continue while input is greater than 0
 while [ $num -gt 0 ]
 do
                #append the numbers in decreasing order with comma-separated
  temp+="$num, "
  num=$(( $num - 1 ))
    done
 temp=$(echo $temp | sed 's/,$//')
 echo $temp


Output:








Sunday, 8 November 2020

Shell Script: Write 000 to 999 (1000 lines in a file) using Brace Expansion

We need to write from 000 to 999 in a file. As these are consecutive numbers we will be using "Brace Expansion"

Let's Create a file by using the touch command.


Script:

 #create an empty file
touch output.txt 

#using brace expansion with foor loop
 for i in {000..999} #this is brace expansion
do
    #append data to output.txt 
    echo $i >> output.txt
done


Output: 

To see the output use bellow command

        cat output.txt | more