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.

marked as duplicate by Gilles shell-script Mar 24 '17 at 23:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 <command> > /dev/null 2>&1 – darvark Mar 24 '17 at 5: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 6: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 6:05
  • 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 7:53
  • 1
    Cool, glad it works now! Please upvote the comments that helped you so people get reputation credit. – MikeD Mar 24 '17 at 8:55
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

  • 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 7 '17 at 0:32
  • @ignatius, tell the shell to run it in the background (see mywiki.wooledge.org/ProcessManagement ), run it in another terminal or another screen or tmux window, ... – ilkkachu Apr 7 '17 at 8:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.