Specify an entire UNIX command as a command line argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Specify an entire UNIX command as a command line argument
# 1  
Old 02-18-2013
Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following

Code:
YN "Specify a question" "doThis" "doThat"

where "doThis" will be executed if the answer is "y", otherwise "doThat".

For example

Code:
YN "Do you want to list the file dog?" "ls -al dog"  ""

Here's my attempt at the script YN

Code:
#!/bin/bash
echo "$1"
read Answer;
if [ "$Answer" = "y" ] ; then
    . "$2"
else
    . "$3"
fi

If I run the above example it returns

Code:
ls -al dog: No such file or directory

Could anybody advise please?
# 2  
Old 02-19-2013
Try this code

Code:
#!/bin/bash

echo -n $1

read ANSWER

if [ "$ANSWER" = "y" -o "$ANSWER" = "Y" ]
then
      $2
else
      $3
fi

you can try executing it in ,
YN "Do you want to list the file dog?" "ls -al dog" ""

The file dog should be in current directory, or else mention the path.
Thanku Smilie
# 3  
Old 02-19-2013
Note that things like variables, wildcards, and backticks won't be expanded inside the command. This is a security feature to prevent people from doing horrible things just by accidentally misusing a variable.
# 4  
Old 02-20-2013
Why not try something like this for you YN script:

Code:
ans=""
while [ -z "$ans" ]
do
    printf "%s" "$1"
    read ans
    case $ans in
        y|Y) true;;
        n|N) false;;
        *)   tput cuu1
             ans="";;
    esac
done

You can then use the if statement to deal with both answers.
Note you can also nest questions and it all looks pretty neat:

Code:
if YN "Do you want to list the file dog? "
then
    ls -al dog
else
    if YN "How about the file cat then? "
    then
        ls -al cat
    fi
fi

---------- Post updated 21-02-13 at 06:25 AM ---------- Previous update was 20-02-13 at 06:56 AM ----------

Also, if you have a single action you can combine use && like this:

Code:
YN "Do you want to list the file dog? " && ls -al dog

or use || if action is for negative response.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command line argument

Hi Guys, I'm trying to work out how to add a command line argument inside single quotes. Would anyone be able to help please as I'm going mad :) I want to be able to place the filename on command line and it then be used in a script but it needs to have quotes surrounding it. Thanks in... (4 Replies)
Discussion started by: mutley2202
4 Replies

2. Shell Programming and Scripting

Checking has for a command line argument

I would like to search and print a match of the user entered $ARGV. Im terrible with hashes and really dont know where to go from here. The example data file name location phone Bugs holeintheground 5551212 Woody holeinthetree 6661313 Jerry holeinthewall 7771414... (4 Replies)
Discussion started by: sumguy
4 Replies

3. Shell Programming and Scripting

Can a string be a command line argument?

I would like to use a string as a command line argument...is this possible using TCSH? For example say my script is called TEST and I would like to pass a string into my script stating why the test failed. EXAMPLE: TEST "Failed due to missing statement" (4 Replies)
Discussion started by: thibodc
4 Replies

4. UNIX for Advanced & Expert Users

Entire Input command not visible in Unix prompt

Hi, While typing the Unix command, entire command is not visible.When the input command is long, it is not visible. I want the entire command to be displayed when i type it. Please help to resolve this issue. Thanks Sampath (7 Replies)
Discussion started by: sampath.giri
7 Replies

5. UNIX for Advanced & Expert Users

command to replace the entire line from the key point

Hi everyone I am new to Unix. I got stuck up by small issue. I have text file something like this abc 'xyz' '5' lmn 'pqr' '7' i want to replace the abc 'xyz' '5' to abc 'xyz' '6' but i have a key as 'xyz' based on this key i want to do that. I am not aware of how to use sed... (7 Replies)
Discussion started by: Vijayaragavan
7 Replies

6. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

7. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

8. Shell Programming and Scripting

How to get the value in last command line argument???

Say I want to get the value of last command line argument using the value in $# (or some other way if u can suggest) how do I do it?? $"$#" `$"$#"` These don't work :( (4 Replies)
Discussion started by: amit_oddey21
4 Replies

9. Shell Programming and Scripting

Capture entire line in ps command

I need to determine what processes are running at certain times of the day. I have a script that issues the /usr/ucb/ps aux command and captures it to a file. I want to see the cpu usage and memory usage. This command lops off the end of the of the display line so I can't see the entire... (2 Replies)
Discussion started by: MizzGail
2 Replies

10. UNIX for Dummies Questions & Answers

array as command line argument !!!!

hello, can any help me how to can pass array as command line argument in korn shell. also how to read a array from command line. thanks spandu (2 Replies)
Discussion started by: spandu
2 Replies
Login or Register to Ask a Question