The function tag has no wiki summary.
0
votes
1answer
13 views
Using variable in KSH function
I tried to have a switch if either an option is set or not
while getopts "s:u:d:e:ch" _OPTION; do
case $_OPTION in
...
c)
isCSet="Y"
then I'm ...
0
votes
1answer
25 views
Function, return value using pwd in KSH
I tried to write a small ksh script:
fDestExists (){
cd /tmp
read vANSWER?" >> Do you want to create a repository in pwd ? Type YES or NO"
echo " |----> $(fGetDatum) You ...
1
vote
1answer
60 views
getopts - how to avoid read next $OPTARG as argument?
Just started use getopts and found one problem...
For example - have a script with:
while getopts "h:loav" opt; do
case $opt in
h)
h=$OPTARG
echo $h
;;
But whet I run it:
$ ./ftpclean.sh -h ...
4
votes
1answer
89 views
How can I colorize head, tail and less, same as I've done with cat?
I've got 'color cat' working nicely, thanks to others
(see How can i colorize cat output including unknown filetypes in b&w?).
In my .bashrc:
cdc() {
for fn in "$@"; do
source-highlight ...
0
votes
2answers
41 views
How to view source of “string.h” and other library function source code in gedit of Ubuntu?
I remember viewing source code of library function but now I don't know how to view it. Can someone help me viewing source code of library function?
0
votes
1answer
59 views
Why does a working standalone nested function/script not work inside a larger script? [duplicate]
The following (nested) function/s
function hpf_matrix {
# Positional Parameters
Matrix_Dimension="${1}"
Center_Cell_Value="${2}"
# Define the cell value(s)
function hpf_cell_value {
...
0
votes
1answer
45 views
Using break command as argument to function [closed]
What about use so solution?
Functions run in loop (cycle?). In that loop - I have another function wuch also uses loop. When second function get NO answer from user - it send break 2 to stop loop and ...
0
votes
0answers
17 views
Allocating Memory to a recursive function [migrated]
I wrote a simple program as below and straced it.
#include<stdio.h>
int foo(int i)
{
int k=9;
if(i==10)
return 1;
else
foo(++i);
open("1",1);
}
int ...
3
votes
3answers
61 views
Is there something like closures for zsh?
I just decided to try zsh (through oh-my-zsh), and am now playing with precmd to emulate a two-line prompt that has right prompts in more than just the last line.
So I clone the default theme, and ...
2
votes
2answers
61 views
bash: get array name from parameter to function with saving indexes
I have a function to show index of chosen element. I'm trying to pass a parameter to function to use it as an array name. This works:
getIndex() {
arrname=$1[@]
b=("${!arrname}")
index=1; while ...
0
votes
2answers
67 views
Why must you be careful when using Bash's built in command history function to re-run previous commands that contain variables?
I know !! re-runs commands but what exactly would occur if I re-ran a command that had a variable in the command?
3
votes
2answers
60 views
How to show last command with expanding function in bash
I'm using function like this.
$ find-grep () { find . -type f -name "$1" -print0 | xargs -0 grep "$2" ; }
After I type:
$ find-grep *.c foo
I want to get expanded last command string. In this ...
5
votes
2answers
164 views
How to test if command is alias, function or binary?
I have command foo, how can I know if it's binary, a function or alias?
1
vote
1answer
53 views
Execute command supplied by function parameters
I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters.
Meaning somethings like this:
special_execute()
{
# Some code
...
2
votes
4answers
106 views
Scope of variables when calling function from find
In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
0
votes
1answer
73 views
Converting a loop of code to function
I want to retry a command for 5 times with an interval of 20 seconds. I want this command to be passed as a method parameter. How to do it? And once the function is written how to pass the value to ...
1
vote
2answers
119 views
Bash function to scp a file not working
I am new to bash scripting and read basic tutorials online and wrote following simple bash function:
function to_company()
{
scp ${1} [email protected]://home/username
}
...
2
votes
3answers
213 views
Bash source — select the right function when two sourced files have the same function name?
My bash script sources a script file (call it file2.sh) according to an argument. (It is either sourced or not.) The script file2.sh contains a function "foo" (call it a modified or improved version ...
1
vote
2answers
693 views
How to return the exit code? Error: return: Reading: numeric argument required
Here's a simplified version of my script. My question is, How do I return the exit code from apt-get in this case?
#!/bin/bash
install_auto() {
apt-get -h > /dev/null 2>&1
if [ $? -eq 0 ] ; ...
4
votes
2answers
95 views
Passing a code block as an anon. function
Is it possible to treat a block of commands as an anonymous function?
function wrap_this {
run_something
# Decide to run block or maybe not.
run_something else
}
wrap_this {
do_something
...
3
votes
1answer
83 views
bash: Accessing function call stack in trap function
Working on a bash function call stack trace...
Script traps errors and runs a callStack() function. But on trapping, It always shows a call stack for the callStack() function itself instead of the ...
2
votes
2answers
52 views
Pass arguments to function exactly as-is
I have the following function:
bar() { echo $1:$2; }
I am calling this function from another function, foo. foo itself is called as follows:
foo "This is" a test
I want to get the following ...
1
vote
1answer
151 views
how to locally redefine 'command_not_found_handle'?
I'd like to make a particular bash script failfast when it cannot find a command, while retaining globally the usual friendly command_not_found behavior. E.g., if I save the following to /tmp/foo.sh, ...
18
votes
4answers
672 views
difference between function foo() {} and foo() {}
I can define bash functions using or omitting the function keyword. Is there any difference?
#!/bin/bash
function foo() {
echo "foo"
}
bar() {
echo "bar"
}
foo
bar
Both calls to functions ...
4
votes
2answers
157 views
Gawk: Passing arrays to functions
Stuck with GNU awk 3.1.6 and think I've worked around its array bugs but still have what looks like a scope problem in a 600-line awk program. Need to verify understanding of array scope in awk to ...
3
votes
1answer
286 views
for loop in bash function
I recently wrote the following bash function:
makeaudiobook () {
count=1
almbumartist=$2
for f in $1; do
preprocess $f > $f-preprocessed
text2wave $f-preprocessed -o $f.wav
...
1
vote
2answers
99 views
Best way to call command within a shell function having the same name [duplicate]
I like to encapsulate commands within shell-functions using the same name. But to avoid the shell-function calling itself recursively, I specify the complete path of the command as the following ...
4
votes
1answer
212 views
How can I create a function in zsh that calls an existing command with the same name?
How can I write a function in zsh that invokes an existing command with the same name as the function itself? For example, I've tried this to illustrate my question:
function ls
{
ls -l $1 $2 $3
...
2
votes
1answer
93 views
How to get functions propagated to subshell?
Solaris / sh
I have a few functions defined in a file which gets loaded via
. ./some_file.sh
When I start a subshell with
sh
All my function definitions are lost but when I do
env
I do ...
5
votes
2answers
120 views
How to define a Bash function that can be used by different scripts
I have defined a bash function in my ~/.bashrc file. This allows me to use it in shell terminals. However, it does not seem to exist when I call it from within a script.
How can I define a bash ...
2
votes
3answers
270 views
What are commands to find shell keywords, built in functions and user defined functions?
I was discussing with my friend on how the commands are parsed in the shell, and he told me that bash searches the command in following order
List of aliases
List of shell keywords
List of ...
8
votes
3answers
238 views
Running an executable in PATH with the same name as an existing function
Sometimes I define a function that shadows an executable and tweaks its arguments or output. So the function has the same name as the executable, and I need a way how to run the executable from the ...
4
votes
2answers
421 views
Bash function not working in Zsh
I have been slowly migrating from Bash to Zsh and have got to the point where everything I have moved across is working well, with one exception.
I have a couple of functions in my .bashrc that I use ...
4
votes
2answers
139 views
Display the function body in bash
I have setup several functions in my .bashrc file. I would like to just display the actual code of the function and not execute it, to quickly refer to something.
Is there any way, we could see the ...
4
votes
1answer
294 views
what is the zsh equivalent of bash's export -f
So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, ...
1
vote
1answer
104 views
How to reference a script-local dictionary in a Vim mapping?
Somehow I'm not able to execute the following mapping:
function! s:MySurroundingFunctionIWantToKeep()
let s:Foobar={'foo': 'bar'}
map \42 :echo <sid>Foobar.foo<cr>
endfunction
call ...
2
votes
3answers
247 views
How to reverse-match a string in the Vim programming language?
I want to find the last index of any character in the [abc] set in the abcabc string but the search should start from the end of the string:
" Returns the 0th index but I want the 5th.
let ...
2
votes
2answers
226 views
Bash: passing braces as arguments to bash function
I love using the following pattern for searching in files:
grep --color=auto -iRnHr --include={*.js,*.html,} --exclude-dir={release,dev,} "span" .
I'd like, however, to have this one wrapped into a ...
4
votes
1answer
124 views
View shell function's current definition
So I am editing bashrc constantly, and I have a terminal open with a working function definition, although bashrc has been updated with a wrong function definition. (Because the definition do not ...
4
votes
3answers
512 views
How to set an alias on a per-directory basis?
Suppose you have an alias go, but want it to do different things in different directories?
In one directory it should run cmd1, but in another directory it should run cmd2
By the way, I have an ...
7
votes
4answers
2k views
Executing user defined function in a find -exec call
I'm on Solaris 10 and I have tested the following with ksh (88), bash (3.00) and zsh (4.2.1).
The following code doesn't yield any result:
function foo {
echo "Hello World"
}
find somedir -exec ...
4
votes
2answers
195 views
Function caller positional parameters
I need to read and write the positional parameters $@ of a function's caller. The Bash man page says that:
A shell function is an object that is called like a simple command and
executes a ...
2
votes
2answers
106 views
Is there any way I can fit this into my ~/.bashrc as a function?
I just discovered this useful bit of code on this useful-looking website.
#!/bin/sh
exec tclsh "$0" ${1+"$@"}
proc main {} {
set lines [lrange [split [read stdin] \n] 0 end-1]
set count ...
8
votes
1answer
822 views
How to get current buffer's filename in emacs?
One of the main features I miss about Vim, is that it always saves the filename of the current file in the % buffer (more info). That allows launching commands easily like:
;; compile current file
:! ...
2
votes
1answer
178 views
awk function with a number parameter for the column you want to print
I want to use my awk shortcut as a function, so that I can pass the column number which then prints me the output. My aliases are:
alias A="| awk '{print \$1}'
alias G="| grep -i'
Instad of typing:
...
2
votes
1answer
72 views
Restoring an option at the end of a function in zsh
I'm writing a zsh shell function (as opposed to a script) where I would really like the extended_glob option to be enabled. But since the function runs in the caller's context, I don't want to clobber ...
3
votes
3answers
579 views
Infinitely Nested Directories
Let me start off by saying this is a Mac Terminal I'm using. Not Linux, but I assumed I would get the best answers here as it has to do with Unix and the command line not really anything about Mac ...
1
vote
1answer
194 views
Switching source and destination (or undoing the mv, cp operation)
mv or cp commands both expect source and destination as arguments.
In case you want to undo the change you made, or just change the source and destination you supplied before, what is the quickest ...
2
votes
1answer
286 views
Why do Unix-like systems execute a new process when calling a new function?
Why do Unix-like systems execute a new process when calling a function rather than a dynamic library? Creating a new process is costly in terms of performance when compared to calling a dynamic ...
2
votes
0answers
313 views
How to enable `sudo` with custom functions?
Recently I learned you can enable sudo for custom aliases as follows:
alias sudo='sudo ' # note: the space is required!
The reason this works is the following:
If the last character of the ...