Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

interactive shell command

Posted on 2011-03-13
14
Medium Priority
?
480 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
  • 6
  • 6
  • 2
14 Comments
 
LVL 81

Assisted Solution

by:arnold
arnold earned 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

581 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