I have used the command alias filecreate='touch $1' in my script to create a new file in my present working directory with the help of filecreate as custom command. But when I execute the script it's showing error. Also how do I make the command to accept 2 parameters one the filename and other the pathname.
alias filecreate=touch
or its function equivalent:
filecreate(){ touch "$#"; }
will accept an arbitrary number of arguments and pass them to touch.
Positional argument expansions usually don't belong in aliases, as aliases, unlike functions, don't get their own positional argument arrays.
Aliases are simple text expansions.
Your
alias filecreate='touch "$1"'
when run like so:
filecreate SomeFile
would simply expand to
filecreate "$1" SomeFile #$1 comes from the caller
This differs from functions and scripts, which do get their own argument array.
Related
I've encountered the following command:
source foo -c configs/foo.config
What is -c flag is actually may do here? foo is bash script, foo.config also looks like one.
The source command is a bash-specific alias for the POSIX-standardized command .. The specific usage you're asking about is mentioned by POSIX as an allowable extension:
The KornShell version of dot takes optional arguments that are set to the positional parameters. This is a valid extension that allows a dot script to behave identically to a function.
Thus, when code read from foo is invoked, its $1 will be -c and its $2 will be configs/foo.config.
This is also explicitly documented if you run help source in bash:
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.
Note the text above: If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.
Background:
I'm trying make a function that runs commands on a set interval because I don't have access to a "watch" program. Simplified to it's most basic from, the function I'm trying to write is runit() { $1; }.
What works:
This works fine and dandy when I pass it things that aren't aliases. For example, runit "ls -l" works fine. I get the full output from the ls -l command.
What doesn't work:
The problem starts when I pass it an alias. For example, setting alias ll="ls -l" then calling runit "ll" will result in -bash: ll: command not found.
Things I have tried:
When I hard-code the alias runit() { ll; }, it works fine and gives me what I expect.
I feel like I might be overlooking something, but I can't quite place my finger on it.
Why would hard-coding the alias work fine, but passing it into the function fail?
Is there a way to accomplish what I'm attempting to do?
From the bash man page discussion of aliases (emphases mine):
Aliases are expanded when a command is read, not when it is executed.
Therefore, an
alias definition appearing on the same line as another command does not take effect until the next line of input is read. The
commands
following the alias definition on that line are not affected by the new alias. This behavior is also an issue when functions are
executed. Aliases are expanded when a function definition is read, not when the function is executed, because a function
definition is
itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed.
To
be safe, always put alias definitions on a separate line, and do not use alias in compound commands.
You can observe this effect in functions by using the type command:
$ run_it () { ll; }
$ type run_it
You should see that the body of the function contains a call to ls -l, not ll.
The last sentence of the section on aliases:
For almost every purpose, aliases are superseded by shell functions.
My interpretation of that line is: if you think you want to use an alias, try writing a function first. Don't use an alias unless the function demonstrably fails to do what you need.
You can use eval like this:
$ runit() { eval $1; }
$ alias ll="ls -l"
$ runit "ll"
eval will expand any alias in $1 before the execution.
One way to solve this problem is to define a shell function rather than an alias.
ll () {
ls -l "$#"
}
The alias is expanded as a macro on command input, whereas the shell function is matched when the command is executed. This is a perfect example of how the shell's macro processor language is good for interactive grace but rather complicates actual programming.
I have one variable, which is coming from some where like:
VAR1='hhgfhfghhgf"";2Ddgfsaj!!!$#^$\'&%*%~*)_)(_{}||\\/'
Now i have command like this
./myscript.sh '$VAR1'
I am getting that $VAR1 from some diff process and when I display it look exactly as its above.
Now that command is failing as there is already single quote inside variable. In the process where I use it it is expanded at that point, which causes that error.
I have control over myscript.sh but not above command.
Is there any way I can get variable inside my script?
What you are saying is not possible to failing when passing to your script. Might your script has processing issue (or a command where this argument will passing into it) which cannot expand the variable correctly. You can either use printf with %q modifier to escape all special characters then pass it to your script:
./myscript.sh "$(printf '%q\n' "$VAR1")"
... or do the same within your script before you wanted to pass to some other commands:
VAR2="$(printf '%q\n' "$VAR1")"
I know I have to take the path first and then run the code like export :$PATH but how do i feed the name of the program to the script?
I have tried to use the command line arguments and try to execute it but it's a dead end.
You can access the first command line argument in the variable "$1", the second with "$2", etc.
To iterate over all the arguments, use
for arg in "$#"; do
do_something_with "$arg"
done
I current read this line from our configure.ac build script. I have search on Google for answer but not find it.
I assume it is shell script but what does this means, especially for -- ?
set -- "$progname" "$#"
From help set:
-- Assign any remaining arguments to the positional parameters.
If there are no remaining arguments, the positional parameters
are unset.
The reason for -- is to ensure that even if "$progname" or "$#" contain dashes, they will not be interpreted as command line options.
set changes the positional parameters, which is stored in $#. So in this case, it appends "$progname" to the beginning of the positional parameters received by the script.
The -- is a bash built-in as well as something a lot of unix commands use to denote the end of command options. So if you have something like:
grep -- -v file
the -v won't be interpreted as a grep option, but a parameter (so you can grep for -v).
The $# is the list of all the parameters that are passed into the script (which I assume the set command is a part of).
The -- ensures that whatever options passed in as part of the script won't get interpreted as options for set, but as options for the command denoted by the $progname variable.