Solved

interactive shell command

Posted on 2011-03-13
14
468 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 76

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 76

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now