3 edited body
source | link

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$""$2\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$""$2\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$""$2\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$""$2\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$""$2\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You could then use function like this

  dir=somewhere/logs
  ext=testfile.json
  cp template "$dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext"

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You could then use function like this

  dir=somewhere/logs
  ext=testfile.json
  cp template "$dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext"

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$2\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$2\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$2\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$2\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$2\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You could then use function like this

  dir=somewhere/logs
  ext=testfile.json
  cp template "$dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext"
2 added 2 characters in body
source | link

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You wouldcould then use this like thisfunction like this

  dir=somewhere/logs
  ext=testfile.json
  cp template $dir"$dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext_$USER_$ext"

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You would then use this like this like this

  dir=somewhere/logs
  ext=testfile.json
  cp template $dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You could then use function like this

  dir=somewhere/logs
  ext=testfile.json
  cp template "$dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext"
1
source | link

Lets break it down into a number of steps.

We are going to write a function that will return the next number. We are going to aim for simplicity, rather than maximal efficiency

You need to give it a directory name

You need to give it the testfile.json part of the filename

We need to find all the files in that directory which end with that ending.

Parameters are passed into a function using $1, $2 etc.

getnum(){
  ls "$1" | grep "$2\$"
}

Use cut to pull out just the second field, using _ as the delimiter. (Note if I was going this I would use sed instead of grep and cut, others might use awk, but we are going for simplicity here).

getnum() {
 ls "$1" | grep "$s\$" | cut -d_ -f 2
}

Use sort to sort them numerically

getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n
}

Use head to get just the first value

 getnum() {
  ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1
 }

Wrap this up so we get the value in a local variable

getnum() {
      local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
}

If there were no values then set the old value to 0. Print out a 2 digit number that is one more that the old one.

 getnum() {
  local oldnum=$(ls "$1" | grep "$s\$" | cut -d_ -f 2 | sort -n | head -1)
  if [ "$oldnum" = "" ]
  then
      oldnum=0
  fi
  printf "%02d\n" $(($oldnum+1))
 }

Note that I could have omitted the test for oldnum not being set, as $((oldnum+1)) would expand to $((+1)) in that case. I can also use expressions like $((${oldnum:-0}+1)), consult the manuals for more information.

You would then use this like this like this

  dir=somewhere/logs
  ext=testfile.json
  cp template $dir/$(date +%Y%m%d_)$(getnum "$dir" "$ext")_$USER_$ext