Help me please to fix my error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help me please to fix my error
# 8  
Old 10-11-2013
hm.....its a short code and I showed examples......ok, thanks....I will try

---------- Post updated at 12:08 PM ---------- Previous update was at 11:32 AM ----------

Example
so I run
sh mycode Manu gg44
And I need to get file with name Manu
with content:
gg44
192.168.1.2. (this number I explain below)

(in the directory DIR=/h/Manu/HOME/hosts there is already file Alex
cat Alex
ff55
198.162.1.1.)

So mycode creates file named Manu with the first line gg44 and generate IP at the second line.
BUT for generating IP he has compare with Alex file IP and new IP has to be more than Alex IP. So second line of Manu has to be 198.162.1.2. If we have more than one files in the directory then we have to check all secon lines of all files and then generate according to them.


Code:
DIR=/h/Manu/HOME/hosts       #this is a directory where i have my files (structure of the files above)
for j in $1 $2             #$1 is Manu; $2 is gg44
do
              if [ -d $DIR ]             #checking if directory exists (it exists already)
              then                        #if it exists
              for i in $*           #  for every file in this directory do operation
              do
              sort /h/ManuHOME/hosts/* |  tail -2 | head -1            # get second line of every file
                IFS="." read A B C D                   # divide number in second line into 4 parts (our number 192.168.1.1. for example)
      if [ "$D" != 255 ]             #compare D (which is 1 in our example: if its less than 255)
     then
     D=` expr $D + 1 `             #then increment it by 1
       else
     C=` expr $C + 1 `          #otherwise increment C and make D=0
     D=0
fi
           echo "$2 "\n" $A.$B.$C.$D." >/h/Manu/HOME/hosts/$1
    done done                  #get $2 (which is gg44 in example as a first line and get ABCD as a second line)

---------- Post updated at 12:10 PM ---------- Previous update was at 12:08 PM ----------

Quote:
Originally Posted by alister
That code is an absolute mess. It's almost as if you're trying to make it difficult for us to help. The very, very least you can do is use sane indentation.

I didn't scrutinize it, but two things stand out in your most recent post. (1) You are not piping sort|tail|head into the while-loop. (2) You don't use $i anywhere (perhaps that's intentional).

On a general note, you have not made any effort to provide useful feedback for the suggestions given. Have you confirmed that the directory exists and that there are files in it, per post #5? If you can't be bothered to help us account for the error messages that you report (when you report them), how do you expect us to help you fix the problems?


I suggest you be specific instead of vague.

Regards,
Alister
Example
sh mycode Manu gg44

(in the directory below there is already file Alex
cat Alex
ff55
198.162.1.1.)

So mycode creates file named Manu with the first line gg44 and generate IP at the second line.
BUT for generating IP he has compare with Alex file IP. So second line of Manu has to be 198.162.1.2. If we have more than one files in the directory then we have to check all secon lines of all files and then generate according to them.


Code:
DIR=/h/Manu/HOME/hosts       #this is a directory where i have my files (structure of the files above)
for j in $1 $2             #$1 is Manu; $2 is gg44
do
              if [ -d $DIR ]             #checking if directory exists (it exists already)
              then                        #if it exists
              for i in $*           #  for every file in this directory do operation
              do
              sort /h/ManuHOME/hosts/* |  tail -2 | head -1            # get second line of every file
                IFS="." read A B C D                   # divide number  in second line into 4 parts (our number 192.168.1.1. for example)
      if [ "$D" != 255 ]             #compare D (which is 1 in our example: if its less than 255)
     then
     D=` expr $D + 1 `             #then increment it by 1
       else
     C=` expr $C + 1 `          #otherwise increment C and make D=0
     D=0
fi
           echo "$2 "\n" $A.$B.$C.$D." >/h/Manu/HOME/hosts/$1
    done done                  #get $2 (which is gg44 in example as a first line and get ABCD as a second line)


In the result it creates file with name Manu and first line, but second line is totally wrong. It gives me ...1.
Also error message
sort: open failed: /h/u15/c2/00/c2rsaldi/HOME/hosts/yu: No such file or directory

yu n ...1.

Last edited by Manu1234567; 10-11-2013 at 01:28 PM..
# 9  
Old 10-11-2013
There are a lot of very fundamental issues with your script.

Quote:
Originally Posted by Manu1234567
Code:
DIR=/h/Manu/HOME/hosts       #this is a directory where i have my files (structure of the files above)
for j in $1 $2             #$1 is Manu; $2 is gg44

The body of this for-loop creates a file and you only need to create one file, so why does this for-loop even exist? Note that this loop makes no use of $j.

Quote:
Originally Posted by Manu1234567
Code:
do
              if [ -d $DIR ]             #checking if directory exists (it exists already)
              then                        #if it exists
              for i in $*           #  for every file in this directory do operation

This inner for-loop does not iterate over every file in the directory. It iterates over the script's command line arguments, of which there are only two, $1 and $2. That issue aside, again, like the outer-loop, this loop does not make any use of its index, $i.

Quote:
Originally Posted by Manu1234567
Code:
              do
              sort /h/ManuHOME/hosts/* |  tail -2 | head -1            # get second line of every file

DIR=/h/Manu/HOME/hosts is not the same as /h/ManuHOME/hosts/*.

Quote:
Originally Posted by Manu1234567
Code:
                IFS="." read A B C D                   # divide number in second line into 4 parts (our number 192.168.1.1. for example)
      if [ "$D" != 255 ]             #compare D (which is 1 in our example: if its less than 255)
     then
     D=` expr $D + 1 `             #then increment it by 1
       else
     C=` expr $C + 1 `          #otherwise increment C and make D=0
     D=0
fi

Again with the lousy indentation. Why are the contents of an if-statement within an inner-for-loop indented less than the outer for-loop?

I see two if-statements but only a single fi keyword. That's definitely not going to work.

Quote:
Originally Posted by Manu1234567
Code:
           echo "$2 "\n" $A.$B.$C.$D." >/h/Manu/HOME/hosts/$1
    done done                  #get $2 (which is gg44 in example as a first line and get ABCD as a second line)

Regards,
Alister
# 10  
Old 10-12-2013
After some guessing about what you want to achieve, I came up with
Code:
DIR="/some/path/"
[ -d "$DIR" ] || { echo "$DIR" not found 1>&2; exit 2; }
sort -nrt. "$DIR"* |
  { IFS="." read A B C D
    [ $((++D)) -ge 255 ] && { ((++C)); D=1; }
    printf "%s\n%s\n" $2 $A.$B.$C.$D > $1
  }

Works if your files' code lines don't sort higher than your IP No.s . Copy that into a script file, adapt to your needs, execute and report back.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need fix for rsync Error due to version mismatch

rsync --delay-updates -F --compress --archive --rsh='/usr/bin/ssh -t -a -x' /web/admin/Transfer/data/ user1@destserver1:/tmp/testf rsync version on sender server is:3.0.9 rsync version on sender server is:3.0.6 Linux sourceserver1 3.10.0-693.17.1.el7.x86_64 #1 SMP Sun Jan 14 10:36:03 EST... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. HP-UX

Help needed HP UX 11.11i new install on C8000 Desktop messaging system error CAN'T FIX

Hi, I have a HP UX C8000 box , have installed 11.11i from 4 x cds numerous times to try to get a functioning Network or to try and get rid of this error on start up, after new install and 1st startup I get an error "The desktop messaging system could not be started" and then advice about... (12 Replies)
Discussion started by: C8000
12 Replies

3. Shell Programming and Scripting

[Solved] Data error need to fix

Hi Guys, I`m having a strange problem with my data set. Whenever there is a transition to another value is col1, the corresponding 3rd col goes to the next line. This is a huge file, so need to fix in a script. The file is tab delimited. Here is what is happening when transitioning from... (4 Replies)
Discussion started by: gina.lizar
4 Replies

4. Windows & DOS: Issues & Discussions

Fix script error (%%n)

Hi, I'm currently tinkering with a script that uses the for command to process all *.gif files in the current folder. I marked the spot where it exits out with an error by adding a "pause" The error message given is: convert.exe: unable to open image `%x': No such file or directory @... (2 Replies)
Discussion started by: pasc
2 Replies

5. AIX

I am getting the following error when I do an errpt. What do I need to do to fix it.

Hi Team, I am getting the following error when I do an errpt. What do I need to do to fix it. LABEL: LVM_SA_STALEPP IDENTIFIER: EAA3D429 Date/Time: Sat 12 Jan 01:10:56 2013 Sequence Number: 880 Machine Id: 00C57B904C00 Node Id: spg-lplaw-01... (1 Reply)
Discussion started by: ranjithm
1 Replies

6. Shell Programming and Scripting

Emergency !! Need to fix this error!

Pls help me with the below script. Its returning an error No such file or Directory. #!/bin/ksh cd /enip/enipapp/cbp/AOC2511201 for file in `cat filename.txt | head -1 | tr -d '\r'` do for i in `cat '$file' | tr -d '\r'` do echo "-----script start `date`... (9 Replies)
Discussion started by: Naga06
9 Replies

7. UNIX for Dummies Questions & Answers

fix the if and then error in bash

For anyone have work in bash shell scripting before know the if and then statement it work like this if (condition); then I have create a mini shell in C, and I want to make it a bit more tolerable than the normal bash shell where you do not need the ';' between if and then if they are in the... (2 Replies)
Discussion started by: snow2462
2 Replies

8. UNIX for Dummies Questions & Answers

How to fix :[too many arguments error in code

I am getting a :; then echo "Enter zero or one file" echo "You must use a valid directory" echo "Current directory is:" pwd exit 0 fi #Flag Variable flag=1 #Code for no arguments if ; then for filename in * do if ; then ... (2 Replies)
Discussion started by: Brewer27
2 Replies

9. Solaris

How to fix df: cannot statvfs error

I have a Sun Sparc machine with Solaris 9 on it. I changed one file system (also mount point) name from /home to /u01. Then I updated /etc/vfstab. After that, I did df -k, system told me that df: cannot statvfs /home: No such file or directory/. Please tell me how and where to fix this error?... (2 Replies)
Discussion started by: duke0001
2 Replies

10. Solaris

Patching error on Solaris 2.6 - I know the 'fix' - do you?

Patching a Solaris 2.6 server (running application that wasn't approved for anything higher) in single user mode - loading about 120 patches. Following error started occurring after patch 105356-23 /kernel//kernel/strmod/ptem: undefined symbol miocpullup ptem error doing common Actual... (0 Replies)
Discussion started by: RTM
0 Replies
Login or Register to Ask a Question