# shell script

**URL:** <https://community.unix.com/t/shell-script/172412>\
**Category:** Shell Programming and Scripting\
**Created:** [June 26, 2007, 2:59pm UTC](https://community.unix.com/t/shell-script/172412 "2007-06-26T14:59:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ciroredz](https://community.unix.com/letter_avatar/ciroredz/32/6_449c533f586939b6e5f246632a46421f.png) [@ciroredz](https://community.unix.com/u/ciroredz)\
**Post date:** [June 26, 2007, 2:59pm UTC](https://community.unix.com/t/shell-script/172412/1 "2007-06-26T14:59:23Z")

</div>

if i do this 5 times  
env \>\> xx  
env \>\> xx  
env \>\> xx  
env \>\> xx  
env \>\> xx  
il will a file called XX with the env redirected into it 5 times

i need to create a script that takes 1 argument being a file, in this instancei ll use the newly created file above xx read the inputted file, in this case xx and look for occurrences of the current user who is executing the script. On finding an occurrence of the username take that line and append it to a file and display a line number and a bracket against the saved line.  
The newly created file should look similar to this  
1) PWD=/home/lee.ballancore  
2) USER=lee.ballancore  
3) MAIL=/var/spool/mail/lee.ballancore  
4) LOGNAME=lee.ballancore  
5) HOME=/home/lee.ballancore  
6) PWD=/home/lee.ballancore  
7) USER=lee.ballancore  
8) MAIL=/var/spool/mail/lee.ballancore  
9) LOGNAME=lee.ballancore  
10) HOME=/home/lee.ballancore

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [June 27, 2007, 2:44am UTC](https://community.unix.com/t/shell-script/172412/2 "2007-06-27T02:44:48Z")

</div>

As a starting point, try :

```nohighlight
env | awk -v User=$USER '$0 ~ User { printf("%d) %s\n", ++count, $0) }'

```

---

<div class="post-metadata">

**Author:** ![ennstate](https://community.unix.com/user_avatar/community.unix.com/ennstate/32/514_2.png) [@ennstate](https://community.unix.com/u/ennstate)\
**Post date:** [June 27, 2007, 2:48am UTC](https://community.unix.com/t/shell-script/172412/3 "2007-06-27T02:48:46Z")

</div>

Hi,  
Check if this is of help.

```nohighlight
#!/bin/ksh
sed -n "/USER=${USER}/,/USER=/p" /tmp/xx | cat -n | sed 's/\([0-9]\{1,3\}\)/\1)/g' > /tmp/${USER}-env

```

Thanks  
Nagarajan Ganesan

---

<div class="post-metadata">

**Author:** ![ciroredz](https://community.unix.com/letter_avatar/ciroredz/32/6_449c533f586939b6e5f246632a46421f.png) [@ciroredz](https://community.unix.com/u/ciroredz)\
**Post date:** [June 27, 2007, 7:59am UTC](https://community.unix.com/t/shell-script/172412/4 "2007-06-27T07:59:58Z")

</div>

been told not to use sed nither awk...that would have been easy!!!

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [June 27, 2007, 8:07am UTC](https://community.unix.com/t/shell-script/172412/5 "2007-06-27T08:07:23Z")

</div>

> [@ciroredz](#):
>
> been told not to use sed nither awk...that would have been easy!!!

Why not use awk/sed ? Is it homework ?

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [June 27, 2007, 8:28am UTC](https://community.unix.com/t/shell-script/172412/6 "2007-06-27T08:28:44Z")

</div>

The logic :

```nohighlight
Initialize counter (value 0)
Display the current environment variables
and For each line do
   If the line contains le username then
      increment the counter
      display counter and line.

```

You can do that with the commands :  
case echo env expr for if read

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [June 27, 2007, 8:46am UTC](https://community.unix.com/t/shell-script/172412/7 "2007-06-27T08:46:23Z")

</div>

Ciro,  
As Vino said, if this is a homework, besides breaking this site rules,  
you are cheating yourself.  
The only way you will learn is to practice.  
If this is not a homework:

```nohighlight
grep "`whoami`" input_file | cat -n

```

---

<div class="post-metadata">

**Author:** ![ciroredz](https://community.unix.com/letter_avatar/ciroredz/32/6_449c533f586939b6e5f246632a46421f.png) [@ciroredz](https://community.unix.com/u/ciroredz)\
**Post date:** [June 28, 2007, 5:24am UTC](https://community.unix.com/t/shell-script/172412/8 "2007-06-28T05:24:39Z")

</div>

I@M WORKING ON IT ON MY OWN!!!!!!!

this is what iv done and it looks good

[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n

and this is part of the output:

```
 1 USER=ciro.ruggiero
 2 MAIL=/var/spool/mail/ciro.ruggiero
 3 PATH=/u01/app/oracle/product/10.2.0/db_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin
 4 PWD=/home/ciro.ruggiero
 5 HOME=/home/ciro.ruggiero
 6 LOGNAME=ciro.ruggiero

```

two questions...AND i@M JUST ASSKING F|OR AN HINT!!!!!!!!

How do i get rif of the 3th line and i do i put a bracket after the number?

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [June 28, 2007, 5:43am UTC](https://community.unix.com/t/shell-script/172412/9 "2007-06-28T05:43:39Z")

</div>

> [@ciroredz](#):
>
> I@M WORKING ON IT ON MY OWN!!!!!!!
> 
> this is what iv done and it looks good
> 
> [ciro.ruggiero@unix ~]$ grep $USER xx | cat -n
> 
> and this is part of the output:
> 
> 1 USER=ciro.ruggiero  
> 2 MAIL=/var/spool/mail/ciro.ruggiero  
> 3 PATH=/u01/app/oracle/product/10.2.0/db\_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin  
> 4 PWD=/home/ciro.ruggiero  
> 5 HOME=/home/ciro.ruggiero  
> 6 LOGNAME=ciro.ruggiero
> 
> two questions...AND i@M JUST ASSKING F|OR AN HINT!!!!!!!!
> 
> How do i get rif of the 3th line and i do i put a bracket after the number?

```plaintext
grep $USER FILE | awk ' NR == 3 { getline; NR=3; } { print NR ") " $0 }

```

---

<div class="post-metadata">

**Author:** ![ciroredz](https://community.unix.com/letter_avatar/ciroredz/32/6_449c533f586939b6e5f246632a46421f.png) [@ciroredz](https://community.unix.com/u/ciroredz)\
**Post date:** [June 28, 2007, 8:22am UTC](https://community.unix.com/t/shell-script/172412/10 "2007-06-28T08:22:58Z")

</div>

how do i put the brackets against the number  
eg:  
1)  
2)

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [June 28, 2007, 8:25am UTC](https://community.unix.com/t/shell-script/172412/11 "2007-06-28T08:25:48Z")

</div>

> [@ciroredz](#):
>
> how do i put the brackets against the number  
> eg:  
> 1)  
> 2)

Did you try the above code?

---

<div class="post-metadata">

**Author:** ![aigles](https://community.unix.com/user_avatar/community.unix.com/aigles/32/118_2.png) [@aigles](https://community.unix.com/u/aigles)\
**Post date:** [June 28, 2007, 9:13am UTC](https://community.unix.com/t/shell-script/172412/12 "2007-06-28T09:13:05Z")

</div>

You will not learn anything if you don't try to resolve the problems by yourself before posting.

> [@ciroredz](#):
>
> How do i get rif of the 3th line and i do i put a bracket after the number?

Read the grep man pages

> [@ciroredz](#):
>
> How do i get rif of the 3th line and i do i put a bracket after the number?

As you don't want to use awk nor sed, you can read the output of the cat command with the read command in a while loop.  
The read command permits to read the first word in a variable and the rest of the line in another one.  
RTFM.

---

<div class="post-metadata">

**Author:** ![ciroredz](https://community.unix.com/letter_avatar/ciroredz/32/6_449c533f586939b6e5f246632a46421f.png) [@ciroredz](https://community.unix.com/u/ciroredz)\
**Post date:** [June 28, 2007, 9:33am UTC](https://community.unix.com/t/shell-script/172412/13 "2007-06-28T09:33:53Z")

</div>

this is the output i was looking for!!!!  
just need to add a bracket on ech number and i do not know how to do it  
[ciro.ruggiero@unix ~]$ grep $USER xx | cat -n

and this is part of the output:

1 USER=ciro.ruggiero  
2 MAIL=/var/spool/mail/ciro.ruggiero  
3 PATH=/u01/app/oracle/product/10.2.0/db\_1/bin:/usr/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ciro.ruggiero/bin  
4 PWD=/home/ciro.ruggiero  
5 HOME=/home/ciro.ruggiero  
6 LOGNAME=ciro.ruggiero

---

<div class="post-metadata">

**Author:** ![reborg](https://community.unix.com/user_avatar/community.unix.com/reborg/32/167_2.png) [@reborg](https://community.unix.com/u/reborg)\
**Post date:** [June 28, 2007, 10:00am UTC](https://community.unix.com/t/shell-script/172412/14 "2007-06-28T10:00:48Z")

</div>

I am closing this thread. Enough information was given in several posts by three memmbers to answer the questions. Other posts have given information about where to look for a solution if you don't like the ones provided. It benefits nobody to continue with this.
