Questions tagged [function]

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.

Filter by
Sorted by
Tagged with
0
votes
2answers
35 views

Bashrc function case statement to import cases from JSON

I'm making a .bashrc function to get me around the system faster. I used a case statement to state where I want to go. function da() { case "$1" in home) cd ~ ;; eolas) cd /...
1
vote
0answers
28 views

Variable not available outside function [duplicate]

I'm having difficulties understanding the following example I created. The variable glob_var gets changed inside the test() function, but the original value remains outside of the function: test() { ...
2
votes
2answers
145 views

How do I unset all functions in zsh?

I know that I can use unset -f $FUNCTION_NAME to unset a single function in bash / zsh, but how do I unset all functions?
-1
votes
0answers
55 views

Is there a utility/tool to “convert” bash functions to zsh functions?

On my new Catalina Macbook, zsh has been designated as the default shell. I've never used zsh, but I'm not exactly fluent in bash either. I have at least 3 options: start learning zsh stick with bash ...
1
vote
0answers
38 views

How do I write a function that has a double colon in a string without my current directory showing up in the string? [duplicate]

function awsl { echo "arg: $1" prfx='arn:aws:sts::' pstfx="$1:assumed-role/FOO/justin@bar.com" role="$prfx$pstfx" echo "role $role" saml2aws login --force --profile=default --role=$role } ...
-1
votes
1answer
21 views

Unable to pass an argument to a function

For my homework, I need to have a function in my code to earn extra credit. I have a script that takes the second argument and makes a folder from what is passed. The code works with the argument ...
1
vote
1answer
87 views

Bash, command as function is not working, but manually is OK

I need save mysql dump from remote machine to file on local, so connect via ssh to remote machine, run mysqldump on remote machine and save it to file on local, where script is running. I have 2 bash ...
0
votes
0answers
10 views

Function connect mysql and launch query

I've made a bash script for making an audit of a server, and I am trying to create a function with the following goal. The function get the mysql parameters (it's a pm file), store as variables and ...
0
votes
2answers
67 views

How to define the name of a bash function to be a regex

I've searched around and can't find an answer. I want to define a function like this function \d{2} () { echo $1_or_smth } Then if I say in my terminal 12 I want it to echo 12 back.
0
votes
1answer
26 views

Run a bash function that takes two files names as variables from command line

I'm running an Abaqus job in Ubuntu command line using two files (file1.inp and file2.f) as follows: abaqus job=file1 user=file2.f Since I'm doing this a lot with different files, I wanted to make ...
0
votes
0answers
29 views

Issue while fetching the records of each csv file in the loop

I have a requirement where I am facing some issues when i am trying to pass the argument to a function. Below is my requirement: I have four kinds of csv file which have a filename with keywords ...
0
votes
2answers
41 views

Bash: Run Command (with a specific pattern) only if another Command (with another specific pattern) has failed

I have a shell script that creates a function (in a bashscript file which is sourced by .bashrc) for each Markdown files to open it in the editor. Example: ls /home/nikhil/Notes/Studies Physics.md ...
3
votes
3answers
106 views

Find exec sh: Shell variable not getting passed to subshell

Here is a simplified code that prints the name of Directory if it contains a Filename with same name as the parent directory and .md extension. FIND(){ find . -type d -exec sh -c ' for d ...
0
votes
1answer
82 views

How to create a bash function from within a .envrc?

I want to use direnv to automatically define a bash function when I switch to a particular directory. Here is the function definition. seqchart () { # Create a sequence diagram creation shorthand ...
0
votes
1answer
43 views

How to pass a multiword string as argument in Linux [duplicate]

I am trying to pass a multiword argument in a function an echo the result in a simple fashion. Currently I am doing this: function myFunction { multiWordString="" for ((i=3; i<=$#; i++...
6
votes
2answers
542 views

zsh: `source` command doesn't reload functions

(This is on MacOS with zsh 5.7.1) Here is how I load custom functions in zsh: # Custom functions fpath=($HOME/.zfunc $fpath) autoload -Uz mackupbackup autoload -Uz tac autoload -Uz airplane autoload ...
0
votes
1answer
118 views

Random unbound variable error within function

I made a function in bash and when I call it, it crashes with an unbound variable error. I don't understand cause the variables that are said to be unbound are declared. Moreover, it seems to be ...
3
votes
1answer
127 views

Get and set the script arguments from within a function in bash

Context: I have an old bash script with a big section parsing its arguments. It happens now that I need to call this section twice, so I plan to move it to a function to avoid code duplication. ...
0
votes
0answers
27 views

make bash function context aware to list files from folder where it's called

As an alias can not be used to send parameters I have created a function in my .bashrc Just for the sake of the example calling chemacs script to keep different emacs distributions side by side. The ...
1
vote
1answer
75 views

Can you help me understand this bash behavior? Background processes and their attachment to the current process

I stumbled upon this unexpected behavior and I was hoping someone with a better understanding could explain it! I have a function that is called by a script and run as a background process - ...
3
votes
1answer
104 views

Auto-completion with shell function

The zsh function cn function cn() { nmcli device wifi connect "$1" --ask } will prompt for a password and connect to the network foo, if you type cn foo But it will not auto-complete foo, as ...
0
votes
1answer
44 views

Access last command of history within a function [duplicate]

I'm trying to create a function that will echo the previous to last command from the current terminal. This is the expected output (here I call the desired function f): $ history 1 history 1 $ f ...
1
vote
2answers
218 views

Sourced Bash script, each with main function

I am using Bash 4.4.20. I typically have main function in each bashscript. If I want to source this script from another function inside another bash script, will this conflict with the main function ...
0
votes
1answer
40 views

Using paths from a function that uses globbing

It is known that parsing the output of ls is generally a bad idea and one solution is to use globbing instead of ls to 'safely' loop through files in a directory. for path in /path/to/search/*; do ...
0
votes
1answer
64 views

Using loop in script for command line arguments

So I am trying to sort files with specific extensions into specific folder (ones that have been chosen by user by command line arguments) Lets say $1 (.jpg) $2 (.docx) etc The script is all working ...
2
votes
1answer
103 views

Why doesn't 'sudo -E' preserve the function environment variables exported by export -f?

In script.sh: #!/bin/bash func(){ echo "I am here a func." } export -f func export variable="I am here a variable" sudo -E bash -c "func ; echo $variable" And the output came: bash: func: ...
1
vote
2answers
90 views

How to cat multiple lines with indentation in bash function?

I have a bash function yumtelegraf() { cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo [influxdb] name = InfluxDB Repository - RHEL \$releasever baseurl = https://repos.influxdata.com/rhel/\...
0
votes
1answer
56 views

Awk function, syntax error near unexpected 'myArr,'

I am not sure why my simple function causes "Syntax error near unexpected 'myArr,'. I taking an array, lookup key value and a line item section name as arguments. Each row in the array is "," ...
1
vote
5answers
113 views

Applying commands to lists

Very often I need to apply a certain simple function to a list (or more precisely to a string where substrings that I want to treat as separate items are delimited by a new line). Say I need to ...
-2
votes
1answer
60 views

Run .bashrc function as sudo [duplicate]

I added this to ~/.bashrc to provide colorized tail: tailc () { tail $@ | ccze } And I can run it as so: tailc -f -n 10 foo/log.txt But I can't use it on a privileged file: sudo tailc -f -n 10 /...
0
votes
1answer
39 views

How to list all function names only, one per line

In ksh93 typeset -f lists all functions and its definition. $ f(){ :; } $ typeset -f f(){ :; } With the added quirk that functions that have been defined without a trailing newline are listed the ...
1
vote
1answer
31 views

Loop function with arguments in another loop function with arguments

# Print $1 $2 times function foo() { for (( i=0; i<$2; i++ )); do echo -n $1 done echo } # Print $1 $2x$3 times function bar() { for (( i=0; i<$3; i++ )); do foo ...
0
votes
0answers
15 views

Can I set an option within a function scope only? [duplicate]

I would like a function to behave as a file / script. For example in the below: #!/usr/bin/env bash github_createRepo() { set -e set -u projName="$(basename "$PWD")" json=$(jq -n \ --...
1
vote
0answers
316 views

'unbound variable' error when using function-level bash trap (trap … RETURN)

Consider this script that calls into a function that sets up a trap for the RETURN of that function: #!/usr/bin/env bash set -euo pipefail function f1() { local temp_file temp_file="$(mktemp)" ...
1
vote
2answers
265 views

Entr: trying to trigger function while monitoring file change

I'm trying to monitor some files with entr. My script based on their examples: do_it(){ echo Eita!; } while true; do ls folder/* more-folder/* | entr -pd do_it; done >> entr: exec do_it: No ...
0
votes
1answer
38 views

$@ in alias inside script: is there a “local” $@?

I have aliased pushd in my bash shell as follows so that it suppresses output: alias pushd='pushd "$@" > /dev/null' This works fine most of the time, but I'm running into trouble now using it ...
1
vote
2answers
267 views

What's the idiomatic way of returning an array in a zsh function?

I have this function, rpargs () { local i args=() for i in "$@" do test -e "$i" && args+="$(realpath --canonicalize-existing -- "$i")" || args+="$i" done } And I ...
0
votes
1answer
42 views

Iterate the if else statement until a condition is success

I have written a below block of code #!/bin/bash TABLE_NAME="${1}" COL_NAME="${2}" FIELD_VALUES_1SQ_FUNC() { FIELD_VALUES_1SQS=`sqlplus -s sab/admin@TERM << EOF SET FEEDBACK ...
2
votes
2answers
161 views

find - how do i make an alias to do something like (find . -iname '*$1*')?

I have a findn function: findn () { find . -iname "*$1*" } Using this function has one downside that I cannot use -print0 | xargs -0 command (I am using mac) following findn filename to extend ...
2
votes
3answers
1k views

Generic function to loop over inputs and execute a command in bash?

I was trying to create a function that loops over inputs and executes a command - regardless of how they are delimited. function loop { # Args # 1: Command # 2: Inputs for ...
0
votes
0answers
15 views

How to edit function from import table in process with gdb

How can I edit function from import table of process to invoke to another address. For example , my process has fopen function in import table , and has read in import table. I want to edit the ...
3
votes
1answer
305 views

What is the use of declare with option -t

If you call the command help declare. You will see the following information: -t NAME : to make NAMEs have the `trace' attribute Is there any example that demonstrates the use of this option. I ...
1
vote
3answers
256 views

Why does a file move/copy function only move one file at a time when using the “*” wildcard?

function mv1 { mv -n "$1" "targetdir" -v |wc -l ;} mv1 *.png It does only move the first .png file it finds, not all of them. How can I make the command apply to all files that match the wildcards?
6
votes
4answers
1k views

Write a function that checks if a string starts with or contains something

I want to write a function that checks if a given variable, say, var, starts with any of the words in a given list of strings. This list won't change. To instantiate, let's pretend that I want to ...
0
votes
1answer
278 views

How to “export” functions in bash?

I have a script, which has a lot of functions inside. myfile1.sh: func1() {} func2() {} I can call them from script with: myfile2.sh: source myfile1.sh func1 func2 But how can I call them if ...
1
vote
1answer
45 views

Redirection to named file descriptor buggy inside functions

Bash has a feature where it will pick a file descriptor number for you and assign it to a variable. $ exec {fd}>foo.txt $ echo "$fd" 11 $ echo "bar" >&$fd $ cat foo.txt bar This feature ...
0
votes
0answers
174 views

Why shell script function can't return negative number? [duplicate]

When I use shell script , function can't return negative number. Why is that please?
2
votes
1answer
242 views

MacOS: Changing screen capture location

I make a lot of presentations that involve many screenshots, and I want an easier way to organize them by project. I'm trying to write a simple function that changes the location where screenshots are ...
0
votes
1answer
105 views

Accessing function documentation

Is there a way to access docstrings in Bash? How does one include a docstring in a function's definition in Bash? More specifically how do I add and access docstrings to the following function? ...
-1
votes
2answers
795 views

sudo without sudo, implying sudo in script

I made some scripts containing some functions which by design needs sudo permission. I have added those path in .bashrc for Linux and .bash_profile for MacOS so that it can be called from anywhere. ...

1
2 3 4 5
9