Results 1 to 7 of 7

Thread: ShellScript: Not able to run the command using Variable

  1. #1
    Join Date
    Sep 2011
    Location
    India
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Unhappy ShellScript: Not able to run the command using Variable

    Hi Folks,

    I was trying to form a command using different variables and assigned to one variable including options.

    Sample:

    CMD="mount"

    CMD=$CMD" /dev/sdb"

    CMD=$CMD" /mnt/disk0/"

    echo "The command is: $CMD"

    $CMD

    Output:
    The command is: mount /dev/sdb /mnt/disk0
    Error: No such file or directory:
    ""

    Note: Both the /dev/sdb and /mnt/disk0 exists.

    If I am running the same command without using variable from the script, it was working fine.

    Please help me to get out of this problem.
    Last edited by psrdotcom; June 1st, 2012 at 07:11 AM. Reason: Updating thread information

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    $()

    You need to tell it to execute. Try this:

    Code:
    $(echo $CMD)
    Vaphell always has good solutions. I hope he sees this thread.

  3. #3
    Join Date
    Sep 2011
    Location
    India
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: ShellScript: Not able to run the command using Variable

    Hi,

    Thanks for the reply.

    But I have tried that as well. But I am getting error like CMD: command not found

    I have put ${CMD}. That also didn't work.

    Any other solution plz.
    Last edited by psrdotcom; June 1st, 2012 at 10:46 AM. Reason: Updated reply

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: ShellScript: Not able to run the command using Variable

    Yes, but you have to provide the full device name. /dev/sdb is just a start, which partition on /dev/sdb are your trying to mount? /dev/sdb1, /dev/sdb2, etc.?

  5. #5
    Join Date
    Sep 2011
    Location
    India
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: ShellScript: Not able to run the command using Variable

    Hi,

    Thanks for the quick reply.

    I have put it as an example content.

    Actually I was trying different command not the which is mentioned the thread.

    I am displaying the command using just before running it.
    It was the correct command.
    I have copy and paste final command string in script and it was working fine.

    What is the problem when I am using with variable.

    ex:
    echo ${CMD}
    ${CMD}

    Output:
    mount /dev/sdb1 /mnt/disk0
    File not found error (When executing the command)

    If I had placed the same command in script
    mount /dev/sdb1 /mnt/disk0
    It is executing perfectly.

    Please let me know the solution.

  6. #6
    Join Date
    Sep 2011
    Location
    India
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: ShellScript: Not able to run the command using Variable

    Solved it.

    I have written the command to a temp file and executed the temp file with in the script.

  7. #7
    Join Date
    Jun 2010
    Location
    the back woods
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: ShellScript: Not able to run the command using Variable

    You're looking for the 'eval' command.

    For example,
    Code:
    $ cmd="ls -l"
    $ eval $cmd

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •