?
Solved

interactive shell command

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

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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
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 79

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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.:
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…
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 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