Computing.Net > Forums > Unix > Unix Shell Program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix Shell Program

Reply to Message Icon

Name: Amit Shukla
Date: April 19, 2003 at 22:21:43 Pacific
OS: Windows
CPU/Ram: 128
Comment:

hi,
i want full solution of following five as shell programe in unix-
1)to search a number in a given list of numbers.
2) to reverse a seven digit number and find the sum of all the digits.
3)to sort a given list of numbers in descending order.
4)to count the number of positive and negative number in a given list.
5)to check that a given number is a perfect number or not.
please help me .kindly send me solution



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: April 21, 2003 at 07:22:19 Pacific
Reply:

OK Bob, I'll take "doing someone else's homework, question 1" for 200, please.

cat number.txt

1
2
3
4
5

var=5
grep $var number.txt
5


0

Response Number 2
Name: nails
Date: April 21, 2003 at 07:54:51 Pacific
Reply:

Hi:

OK, I'll do #3:

Using David's number.txt file:

sort -nr number.txt

Let assume number.txt has more than one column, and we're sorting by the 3rd column:

sort -nrk3,3 number.txt
Regards,

nails


0

Response Number 3
Name: Jigujigu
Date: April 22, 2003 at 09:53:55 Pacific
Reply:

Hi,
My attempt on #2.

To reverse a number do something like:

number=1234567
echo $number | rev

To find sum of all digits do something like:
echo 1234567 | rev | read number
sum=0
while [[ $number > 0 ]]
do
t1=$((number/10))
t2=$((t1*10))
reminder=$((number-t2))
number=$((number/10))
sum=$((sum+reminder))
done
echo $sum


0

Response Number 4
Name: James Boothe
Date: April 22, 2003 at 10:16:08 Pacific
Reply:

OK, guess I'll take #2 ...

#!/bin/ksh

if [ $# -ne 1 ] ; then
   echo 'Need one parameter: 7-digit number'
   exit 1
fi

number=$1

if [ $(expr $number : "[0-9]*$") -ne 7 ] ; then
   echo 'Sorry, I need exactly 7 DIGITS'
   exit 1
fi

reversed=$(echo $number|rev)

echo $reversed |
   sed "s/./&+/g;s/.$//" |
   bc |
   read total

echo "\n  number:      $number"
echo "  reversed:    $reversed"
echo "  digit sum:   $total\n"

exit 0

$ ./prob2.sh 1234567

  number:     1234567
  reversed:    7654321
  digit sum:   28


0

Response Number 5
Name: James Boothe
Date: April 22, 2003 at 10:20:47 Pacific
Reply:

Sorry Jigujigu, your reply was not there when I started. Looks like a good solution, and pure shell, whereas I used sed.


0

Related Posts

See More



Response Number 6
Name: David Perry
Date: April 24, 2003 at 12:45:35 Pacific
Reply:

For #5, you need to know what is and is not a prime number.

#!/bin/sh
IFS=':'
export IFS
i=1 ;
while [ $i -lt 100000 ] ; do
if [ `factor $i | awk '{ print $2 }'` -eq $i ] ; then
echo "$i is prime" ;
fi ;
((i=i+1)) ;
done

http://home.pacific.net.sg/~novelway/MEW2/lesson1.html


0

Response Number 7
Name: jairasri
Date: April 28, 2003 at 00:18:26 Pacific
Reply:

hai,
i want shell progrm in unix to find the sum
of the first 3 digit in a 7 digit number
please help me, kindly send me solution


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix Shell Program

Unix Shell program www.computing.net/answers/unix/unix-shell-program/3441.html

Shell Programming www.computing.net/answers/unix/shell-programming/1017.html

unix shell programs www.computing.net/answers/unix/unix-shell-programs/4925.html