Solved

interactive shell command

Posted on 2011-03-13
14
474 Views
Last Modified: 2012-06-22
Hello,

I'd like to write the following shell command

for i in `find / -name '*.txt'`; do echo $i; "wait for my command (e.g. mv $i mydir, or rm $i) ENTER; execute my command"; done

Do you know the what to write between "" instead of my explanation? thank you
0
Comment
Question by:xRalf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
14 Comments
 
LVL 78

Assisted Solution

by:arnold
arnold earned 100 total points
ID: 35122330
This will prompt you for each file.
for i in `find / -name '*.txt'`; do 
echo $i;
echo -n "Please enter command: ";
read a; 
if  [ ! -z "$a" ] ; then 
     if [ "$a" = 'mv' ] ; then 
           $a $i mydir; 
     else if [ "$a" = 'rm' ] ; then 
           $a $i;
     fi;
     fi;
fi;
done

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35122332
Like this?

#!/bin/sh
for i in $(find / -type f -name "*.txt")
  do
    echo "Enter command for \$i (= $i)"
    read cmd
    eval echo "$cmd - OK? \(Y/y to execute\)"
    read OK
      [[ $OK = y || $OK = Y ]] && eval "$cmd"
  done
exit
0
 
LVL 6

Author Comment

by:xRalf
ID: 35122430
Both solutions seem to be correct. I prefer woolmilkporcs solution because it's more general and I wanted it to be more general,

but there is probably some mistake in [[$OK ... line

script.sh: 1: [[y=y: not found
script.sh: 1: y=Y: not found

I tried to write $OK=y (without space, but it didn't help)

Would it be possible to press only ENTER? (not y/Y)?
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35122449
Did you leave a space between "[[" and $OK?

Please post your script!

wmp
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 400 total points
ID: 35122476
>> Would it be possible to press only ENTER? (not y/Y)? <<

Sure:

#!/bin/sh
for i in $(find / -type f -name "*.txt")
  do
    echo "Enter command for \$i (= $i)"
    read cmd
    eval echo "$cmd - OK? \(ENTER to execute, anything else to proceed to the next file\)"
    echo "Ctrl-C to exit"
    read OK
      [[ -z $OK ]] && eval "$cmd"
  done
exit
0
 
LVL 6

Author Comment

by:xRalf
ID: 35122485

#!/bin/sh
for i in $(find ~ -type f -name "*.txt")
do
        echo "Enter command for \$i (=$i)"
        read cmd
        eval echo "$cmd - OK? \(Y/y to execute\)"
        read OK
         [[ $OK = y || $OK = Y ]] && eval "$cmd"
done
exit

Open in new window

0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35122509
Looks good. Seems that you inserted the missing space. Does it work now?
0
 
LVL 6

Author Comment

by:xRalf
ID: 35122626
No there is still error, I will try the solution with enter, but I'm intereted in where the error is.
0
 
LVL 6

Author Comment

by:xRalf
ID: 35122647
Solution with ENTER has that error too.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35122747
Well,

since it works for me (I tested with bash and ksh) I can only assume that your shell doesn't support "[[ ... ]]"

So try this:

#!/bin/sh
for i in $(find / -type f -name "*.txt")
  do
    echo "Enter command for \$i (= $i)"
    read cmd
    eval echo "$cmd - OK? \(Y/y to execute\)"
    read OK
       [ "$OK" = "y" -o "$OK" = "Y" ] && eval "$cmd"
  done
exit

or:

#!/bin/sh
for i in $(find / -type f -name "*.txt")
  do
    echo "Enter command for \$i (= $i)"
    read cmd
    eval echo "$cmd - OK? \(ENTER to execute, anything else to proceed to the next file\)"
    echo "Ctrl-C to exit"
    read OK
      [ -z "$OK" ] && eval "$cmd"
  done
exit
0
 
LVL 6

Author Comment

by:xRalf
ID: 35122853
With bash it works, I thought I have to use sh, because of
#!/bin/sh

But the script does not work correctly if the file has spaces in it, or dots (.) in it

e.g.

my first file.txt
my.second.file.txt
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35122956
Sure it works with dots! You must be mistaken here!

As for the spaces - OK, if there are really spaces you must use "$i", as in

mv "$i" mydir

and "sh" - in most Unix/Linux versions /bin/sh is a link to the system's default shell, in Linux mostly bash, other Unixes often use ksh.



0
 
LVL 6

Author Comment

by:xRalf
ID: 35122971
>> Sure it works with dots! You must be mistaken here!

Maybe I've mistaken. I believe you.
0
 
LVL 78

Expert Comment

by:arnold
ID: 35122973
you can follow my script and customize it as you see fit.
The script was a direct response to what you wanted to achieve.
The difficulty is that different commands require different number of parameters.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question