1

I have a shell script:

#!/bin/bash
for name in /home/imp/imp/msgs/$1.PK1; do
    mv "$name" "${name%.PK1}.BRD" 2>/dev/null >/dev/null
done
for name in /home/imp/imp/msgs/$1.PK2; do
    mv "$name" "${name%.PK2}.MIX" 2>/dev/null >/dev/null
done

It works, but only on pre-existing files. What happens, is that more *.PK1 and *.PK2 are created after the initial scan. I'd like this script to "loop" and rename the files that are created afterwards. Is this possible?

UPDATE:

This is what I have now:

#!/bin/bash

while [ ! -z "$(ls *.PK1 *.PK2 2>/dev/null)" ]; do

for name in /home/imp/imp/msgs/$1.PK1; do
    mv "$name" "${name%.PK1}.BRD" 2>/dev/null >/dev/null
done
for name in /home/imp/imp/msgs/$1.PK2; do
    mv "$name" "${name%.PK2}.MIX" 2>/dev/null >/dev/null
done
sleep 1; done

Is that correct?

Thanks.

ignatius
  • 391
  • 3
  • 6
  • 13
  • Please show usage of this script. And `2>/dev/null >/dev/null` what is that. if you want to redirect stdout and stderr to /dev/null just use ` > /dev/null 2>&1` – darvark Mar 24 '17 at 05:51
  • So you want this script to be started once and then loop forever and rename all newly created `.PK#` files? Why not place a `while true; do` and `sleep 1; done` around your stuff? Adapt the sleep according to your expectations on how long it may take until new files will be renamed. But don't even think of removing it. – Philippos Mar 24 '17 at 06:01
  • 1
    Put it all inside a while loop that tests for the existence of *.PK1 or *.PK2, like so: `while [ ! -z "$(ls *.PK1 *.PK2 2>/dev/null)" ]; do [YOUR CODE HERE]; sleep 1; done' – MikeD Mar 24 '17 at 06:05
  • Good idea, but where to I place thew `while true; do`and `sleep ;1' at? – ignatius Mar 24 '17 at 06:05
  • Where do I place the `while [ ! -z "$(ls *.PK1 *.PK2 2>/dev/null)" ]; do` at? – ignatius Mar 24 '17 at 06:11
  • Nevermind. I figured it out. – ignatius Mar 24 '17 at 06:32
  • 1
    Your script will now rename files and stop once there are no new files for once second. Is that your desired behaviour? – Philippos Mar 24 '17 at 07:53
  • The problem is that it's not stopping. – ignatius Mar 24 '17 at 08:07
  • Ok. I know the problem. It's that the script takes an argument. I need to make it scan through all of the `*.PK1` and `*.PK2` files. – ignatius Mar 24 '17 at 08:13
  • OK. I changed `$1.PK1` and `$1.PK2` to `*.PK1` and `*.PK2`, the problem is still that the script doesn't terminate. – ignatius Mar 24 '17 at 08:18
  • Ok. I figured it out. I'm closing this thread. Thanks, all. – ignatius Mar 24 '17 at 08:49
  • 1
    Cool, glad it works now! Please upvote the comments that helped you so people get reputation credit. – MikeD Mar 24 '17 at 08:55
  • @MikeD comments give no reputation. And answers belong in answers, not comments. Comments can be deleted with no warning and should only be used to ask for clarification. Also, using `ls` like that is very bad practice. See ikkachu's [answer](http://unix.stackexchange.com/a/353522/22222) for a way to do it safely. – terdon Mar 24 '17 at 09:40

1 Answers1

2

Is there some reason to use $1 in for name in /path/$1.PK1 instead of just for name in /path/*.PK1? Do you pass a glob pattern to the script on purpose?

If you want to catch all files with those suffixes and new ones as they appear, a simple solution in Bash would something like this:

shopt -s nullglob
dir=/home/imp/imp/msgs
while true ; do
    for name in "$dir/"*.PK1; do
        mv "$name" "${name%.PK1}.BRD" 2>/dev/null >/dev/null
    done
    for name in "$dir/"*.PK2; do
        mv "$name" "${name%.PK2}.MIX" 2>/dev/null >/dev/null
    done
    sleep 1
done

If you want to avoid doing extra work, you could (on Linux) use something like inotifywait to wait for new files to appear, e.g. as in here: How to use inotifywait to watch a directory for creation of files of a specific extension

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • One last question regarding this script: can it run in the background, while another program is running? If not, any way I can make it do so? – ignatius Apr 07 '17 at 00:32
  • @ignatius, tell the shell to run it in the background (see http://mywiki.wooledge.org/ProcessManagement ), run it in another terminal or another `screen` or `tmux` window, ... – ilkkachu Apr 07 '17 at 08:43