Help me please to fix my error

Can you help me to fix my error please?! I checked the code and I think there is no mistake, but when I run it gives me error such
line 1: /Users/Manu/trials/hosts: is a directory
sort: open failed: /Users/Manu/trials/hosts/*: No such file or directory
Help me please...

mycode NAME value.
It creates file with NAME and value inside the file (first line).
Second line iin file is number generated by my code.
ex:
my_code Manu rtry466ry
-> cat Manu
rtry466ry
192.168.1.1.

mycode:

DIR=` /Users/Manu/trials/hosts `
 for i in $1 $2
        do
        if [ "$(ls -A $DIR)" ]
              then
              sort /Users/Manu/trials/hosts/* | head -2 |  tail -1 |
                    while IFS="." read A B C D
                    do
                    if [ $((++D)) -eq 255 ]
                        then
                        ((C++));
                         D=0;
                    fi
    
              echo $2 "\n" $A.$B.$C.$D. >/Users/Manu/trials/hosts/$1
              done
          else 
            echo $2 "\n" "192.168.1.1." >/Users/Manu/trials/hosts/$1
              fi
              done

Use double quote instead of backquote :

DIR="/Users/Manu/trials/hosts"

I have changed but my file contains different things.....Any other suggestions? Maybe mu loops doing wrong stuff?

ls /Users/Manu/trials/hosts/* | sort | head -2 |  tail -1 |\

I think those err msgs are self explanatory and correct. Try to check your files/directories.

As I can't relate your spec text to your script, I'm afraid I can't help further. Just an aside: Be aware that IP host numbers 0 and 255 are special addresses not to be used generally.

I cant fix it people. Help me please. Really confused....
It gives me nothing but ideally:

sh mycode Manu tt-44-44
It creates file Manu
cat Manu
tt.44.44
192.168.1.1.

sh mycode Chris gg-22-22
It creates file Chris
cat Chris
gg-22-22
192.168.1.2.
But my code even does not increment last digit even its clear in code that i increment it..... Help please ;(

DIR="/h/Manu/HOME/hosts"
 for i in $1 $2
        do

        if [ -d "$DIR" ]
              then
              sort /h/Manu/HOME/hosts/* |  tail -2 | head -1
                    while IFS="." read A B C D
                    do

   if [ $((++D)) -eq 255 ]
   then
   ((C++));
   D=0
 #     if [ "$D" != 255 ]
  #   then
   #  D=` expr $D + 1 `
    #   else
     #C=` expr $C + 1 `
    # D=0


fi




              echo $2 "\n" $A.$B.$C.$D >/h/Manu/HOME/hosts/$1

 done
          else
            echo $2 "\n" "192.168.1.1" >/h/Manu/HOME/hosts/$1
              fi
              done

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

1 Like

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.

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 ----------

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.

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.

There are a lot of very fundamental issues with your script.

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.

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.

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

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.

Regards,
Alister

After some guessing about what you want to achieve, I came up with

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.