[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

interactive shell command

Posted on 2011-03-13
14
Medium Priority
?
477 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 80

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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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 80

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…
Suggested Courses

656 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