Naming scripts in Linux run variables

Source: Internet
Author: User

single="ls -l"

$single

=============

multi="ls -l | grep e"

echo $multi > tmp.sh
bash tmp.sh

=============

cmd="ls -l | grep e"
bash -c "$cmd"

=============

$ cmd="echo foo{bar,baz}"
$ $cmd
foo{bar,baz}
$ eval "$cmd"
foobar foobaz

=============

=============

eval "$1"Executes the command in the current script. It can set and use shell variables from the current script, set environment variables for the current script, set and use Functions from the current script, set the current directory, Umask, limits and other attributes for the current script, a nd so on. bash "$1"executes the command in a completely separate script, which inherits environment variables, file descriptors and oth ER process environment (but does does transmit any change back) but does not inherit internal shell settings (shell variabl ES, functions, options, traps, etc.).

There is another to, (eval "$1") , which executes the command in a subshell:it inherits everything from the calling script but does not transmit any.

For example, assuming that the variable dir isn ' t exported $1 cd "$foo"; ls and are, then:

    • cd /starting/directory; foo=/somewhere/else; eval "$1"; pwdLists the content of and /somewhere/else prints /somewhere/else .
    • cd /starting/directory; foo=/somewhere/else; (eval "$1"); pwdLists the content of and /somewhere/else prints /starting/directory .
    • cd /starting/directory; foo=/somewhere/else; bash -c "$1"; pwdLists the content of /starting/directory (because cd "" doesn ' t change the current directory) and prints /starting/directory .

=============

Note: The script file must be in UNIX format; there are no blank lines in the script file, no comments;

cat/studies.sh | While the read line does while [1-eq 1] do declare-i filelines filelines= ' qstat-u hy | grep LQ |       Sed-n ' $= ' echo-e "$fileLines \c" if (($fileLines <50) then echo "$line" eval "$line" pwd Break else sleep + fi done

REF:

Http://stackoverflow.com/questions/3469705/how-to-run-script-commands-from-variables

Http://stackoverflow.com/questions/4668640/how-to-execute-command-stored-in-a-variable

Http://unix.stackexchange.com/questions/124590/variable-as-command-eval-vs-bash-c

Http://superuser.com/questions/679958/execute-a-command-stored-in-a-variable

Http://www.cyberciti.biz/tips/howto-running-commands-from-a-variable.html

Http://www.tldp.org/LDP/abs/html/commandsub.html

http://ubuntuforums.org/showthread.php?t=1018434

Naming scripts in Linux run variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.