Questions on function usage in the context of Unix & Linux (mostly but not exclusively shell scripts). Questions on programming in Python, Perl, Ruby, etc. should be asked on Stack Overflow.

learn more… | top users | synonyms

0
votes
0answers
13 views

Using spd-say in a bash script function

I'm sure this is fairly elementary, but I can't figure it out being as I'm mostly a complete beginner to the nuances of linux shell scripting. Perhaps someone would like an easy one? My script: #!/...
1
vote
1answer
38 views

How to split Bash array into arguments

I wrote a bash script for listing python processes, ram usage and PID and status in human readable form with colored lines. But I have some trouble with working time of the script. Because of the ...
39
votes
11answers
4k views

Why write an entire bash script in functions?

At work, I write bash scripts frequently. My supervisor has suggested that the entire script be broken into functions, similar to the following example: #!/bin/bash # Configure variables ...
1
vote
2answers
38 views

bash function : splitting name and extension of a file

I have the following function split in my .bash_profile file. function split { name="${$1%.*}" ext="${$1##*.}" echo filename=$name extension=$ext } Now I should expect that the command ...
1
vote
1answer
19 views

Bash function with arguments

I know I can write bash scripts like: foo() { echo $1; } but can I define a function that writes: foo(string) { echo $string; } I just can't find my way out of this.
0
votes
1answer
32 views

Function added to .bashrc doesn't work [duplicate]

I've added this function to my .bashrc file: SSH(){ ssh -X -o PasswordAuthentication=yes [email protected].$* } and while trying to perform for ex. SSH 101 I get message: SSH: command not found ...
1
vote
2answers
43 views

How to improve function for simplifying history command

I came up with this snippet to simplify use of history and prevent flooding of the scroll buffer: h() { if [ $# -eq 1 ]; then history | grep $1 | tail -n $(expr $(tput lines) - 1) ...
1
vote
1answer
47 views

bash function to execute a command as argument

I'm writing a bash script and I have a function that gets 3 arguments, a hostname, a command, and a file, it should excute the command on that hostname and redirect the output to the filename. This is ...
1
vote
2answers
38 views

Forbid use of specific commands

I develop tools on a cluster. Each user loads common environment files in their .bash_profile. I have a login node on which certain tools should not run. How can I prevent users from using those tools ...
1
vote
1answer
24 views

How to execute 'find' with 'sed' within a bash function

I'm trying to write a simple bash function to search-and-replace recursively down a directory, changing one string to another. Here's what I've got: function sar () { from="$1" shift to="$...
0
votes
1answer
36 views

System/ X freezes graphically on function keys press

Not entirely sure why, but shortly after running a system upgrade, ie. sudo pacman -Syyu, it appears that my entire graphical environment freezes on the keypress of any of half my function keys, such ...
1
vote
1answer
17 views

Loop to apply command to 300 files in a directory and rename the output including the original input file name plus new text

I have a code that is svr_vars. I have a directory with 300 different files. I want to loop it and to run the code on each file in the directory. The svr_vars code outputs a generic filename of ...
19
votes
3answers
1k views

Do functions run as subprocesses in Bash?

In Advanced Bash-Scripting Guide, in example 27-4, 7-th line from the bottom, I've read this: A function runs as a sub-process. I did a test in Bash, and it seems that the above statement is wrong....
3
votes
1answer
65 views

Circular name references in bash shell function, but not in ksh

I'm writing a set of shell functions that I want to have working in both Bash and KornShell93, but with Bash I'm running into a "circular name reference" warning. This is the essence of the problem: ...
1
vote
2answers
38 views

How do I reference an original command, so I can replace it with a function

So I am trying to create a simple function to replace the standard who command with my own, similar to a function I use to replace the standard cd command. Goal: Replace original who command with who ...
2
votes
1answer
113 views

Bash function that accepts input from parameter or pipe

I want to write the following bash function in a way that it can accept its input from either an argument or a pipe: b64decode() { echo "$1" | base64 --decode; echo } Desired usage: $ b64decode ...
2
votes
0answers
77 views

Exported bash functions sometimes visible from Perl

My Redhat 9, OpenBSD 4.9, FreeBSD 10, Macos X, LinuxMint 17.3, and Ubuntu 14.04.4 all print OK when running this: myfunc() { echo OK; } export -f myfunc perl -e open\(\$fh,\"\|-\",\"@ARGV\"\)\;close\$...
4
votes
2answers
174 views

Return on error in shellscript instead of exit on error

I know that set -e is my friend in order to exit on error. But what to do if the script is sourced, e.g. a function is executed from console? I don't want to get the console closed on error, I just ...
1
vote
2answers
66 views

How can you pass parameters to function in a script?

I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable ...
1
vote
1answer
55 views

Func name as variable in loop

Overview: I save my variable in a config file and call them later. Each entry with the name FailOverVM has a number beside it like FailOverVM1 and I want to check to see if it has data and generate a ...
1
vote
1answer
85 views

Segmentation fault when calling a recursive bash function

I have hundreds of multiple folders which contains thousands of zip files which contain nested within the zip files like show on three below start tree structure 012016/ ├── 2016-01 │   └── 2016-01 │ ...
1
vote
2answers
284 views

Syntax Error near Unexpected Token in a bash function definition [closed]

Forgive me; I'm pretty new to bash files and the like. Here is a copy of my .bashrc: alias k='kate 2>/dev/null 1>&2 & disown' function kk {kate 2>/dev/null 1>&2 & disown}...
1
vote
1answer
54 views

Save return value from a function in one of its own parameters

This is what I want to achieve: Function: Func1() { $1="Hello World" } Call function: local var1 Func1 var1 echo $var1 (should echo Hello World) I found this example which seems to work, but ...
4
votes
0answers
40 views

SIGINT is not cleaned up in “${FUNCNAME[@]}”

Take the following script, interrupted by keyboard input ^C as shown: $ function a() { echo "Performing"; sleep 10; echo "Performed"; } $ a Performing ^C $ echo "${FUNCNAME[@]}" a source If we ...
6
votes
1answer
60 views

Displaying usage comments in functions intended to be used interactively

I have a number of functions defined in my .bashrc, intented to be used interactively in a terminal. I generally preceded them with a comment describing its intended usage: # Usage: foo [bar] # Foo'...
1
vote
1answer
34 views

$* variable of zsh function leads to unexpected results

I have this function (defined inside my ~/.zshrc): function graliases { if [[ "$#*" -lt 1 ]] then echo "Usage: graliases <regex>" else echo "$*" grep -E '*"$*...
1
vote
2answers
34 views

VIM: function that checks if external program is running

Using a vim function, I would like to check if a program is running using pgrep, and if it is not running then do something. In particular, I want to achieve something like this: function! ...
1
vote
0answers
35 views

Returning an array from a ksh93 function

I would like to return an array from a function in ksh93. At this point, using the following method, the contents of the array are passed as a single element even though, in the fonction, the array ...
1
vote
1answer
27 views

Caller-aware function in bash

I have a function (=callee) that should effectively declare and assign a couple of variables in its caller. It should also be able to tell what it's caller's name is. For now I achieve the former by ...
0
votes
1answer
39 views

retrieve numbers with 0 for a single int value

In my script, I need to get a 0 for numbers <=9. When the user types any number, I add 5 to it and if the result is less than 9, I need to print a 0 in order to have a 2-digit number (like 07,08,09 ...
1
vote
1answer
191 views

Script to send mail using function

I am trying to write a bash script with a function which you use to send an email from the command line to an address and include a Cc address, a subject line, and an input file. For example, if the ...
1
vote
0answers
214 views

zsh function: maximum nested function level reached

Consider this simple function I am trying to add in my .aliases (dnf is Fedoras newest replacement of Yum): function abc() { dnf search something } It works well on Bash but I get the following ...
4
votes
4answers
57 views

Does \ work for escaping functions?

I have a function defined in my .bashrc that I would like to bypass: function func() { // func } export -f func When I run env -i func I can access the func command without the function in the ...
0
votes
2answers
178 views

Why should not I update to most recent kernel immediately after release [closed]

Question intended for system administrators. Consider system running a old but working kernel and all the required functionality is available. (Ubuntu 12.04 LTS specifically with kernel 3.2) Then a ...
0
votes
3answers
234 views

Defining bash function dynamically using eval

I'm trying to define a bash function dynamically using following code: delegate_function() { echo "output from delegate"; } eval "parent_function() { echo $(delegate_function); }" The intent is to ...
1
vote
2answers
251 views

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of ...
0
votes
2answers
58 views

Please explain below bash function

I find this function online. It's does creating a directory and changing to directory. But I want to know every part of it. function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\""; }
1
vote
3answers
242 views

Using time on bash functions (not commands)

How can one measure individual calls to bash functions from inside the bash file. I have a program that I call using the command eclipse -b col_solve.pl -e "myPred" This call outputs some ...
1
vote
3answers
50 views

case statement not behaving as expected (fuzzytime() function)

FuzzyTime() { local tmp=$( date +%H ) case $((10#$tmp)) in [00-05] ) wtstr="why don't you go to bed" ;; [06-09] ) wtstr="I see your very eager to start the day" ...
0
votes
1answer
47 views

Is there a Fish Function I can make to eliminate leading “$”/“#” from commands copied from sites?

Sometimes when I'm copying and pasting a command from a site, I accidently copy the leading "$" or "#" by accident. Is there a Fish Function I could make that would check if one of those is included ...
0
votes
2answers
23 views

ZSH function to edit a file based on an input at the cli

I need to set up a function in zsh that would edit a different file based on some input at the command line. I want to simplify my aliases so I don't have multiple aliases to do the same thing but ...
2
votes
4answers
240 views

How to catch and handle nonzero exit status within a Bash function?

Say I have the following (pointless) Bash function: myfunc() { ls failfailfail uptime } I run it as follows: myfunc || echo "Something is wrong." What I want to happen is ls runs (as ...
5
votes
1answer
83 views

Cannot grep jobs list when jobs called in a function

I can grep the output of jobs, and I can grep the output of a function. But why can't I grep the output of jobs when it's in a function? $ # yes, i can grep jobs $ jobs [1]+ Running vim [2]+...
2
votes
2answers
37 views

Get specific result from function

Is there a way to return a specific value in an echoing function? return allows me to return an exit status for the function. I need to return a more sophisticated data structure such as an array, or ...
0
votes
1answer
121 views

netcat daemon for calling functions in sh script

I'm new to shell programming and I have created a script that opens a connection to a server of mine. I want to have this script listen for an input from a client node and use that to run a function. ...
0
votes
0answers
18 views

How do I compute the angle of the 2D 3 points ? [[ In kernel space]

I 'm looking at the touch driver 's Kennel. Sometimes a touch is bouncing County singah appear. Collected by recruiting three points 'm trying to calculate the angle between . But there is no such ...
0
votes
0answers
34 views

How do i run multiple scripts within another script in the same directory?

Last part of a project and I've created 3 scripts already that do the following, but I now need to run them within a 4th script as if it was one program. Below are the instructions as well as the code ...
1
vote
2answers
48 views

Optional parameters in bash function

I have a function for quickly making a new SVN branch which looks like so function svcp() { svn copy "repoaddress/branch/$1.0.x" "repoaddress/branch/dev/$2" -m "dev branch for $2"; } Which I use to ...
1
vote
1answer
61 views

how to get or reflect the name of the bash function which is called? [duplicate]

i did not yet found a solution to this. Anyone a hint? i sometimes write bash functions in my shell scripts and i love to have my scripts being verbose, not just for debugging. so sometimes i would ...
1
vote
1answer
45 views

Which shells have functions where “local” does not alter exported variables for child processes?

In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the ...