2
votes
2answers
51 views

Call the script after disconnecting from server

I have a script I made for work that will call a function that takes an argument. I use the arguments to ssh into our servers. My question is: Is there a way to call the method so that if/when I get ...
2
votes
1answer
39 views

How can I get this script file's functions to load without having to source it every time? “command not found” (Bash/scripting basics)

How can I get this script file's functions to load without having to source it every time? I created a file foo with script functions I'd like to run. It's in /usr/bin, which is in the PATH. File ...
2
votes
2answers
106 views

How to force watch to run under bash

The problem is that when watch is executed it runs sh and I get this error: sh: 1: func1: not found here is the code: #!/bin/bash func1(){ echo $1 } export -f func1 watch func1
6
votes
1answer
52 views

BASH return to main function

I have a BASH script that calls a function, which calls other functions: #!/bin/bash function foo { function bar { # do something } bar } foo How can I return from bar ...
0
votes
2answers
73 views

Exit the bash function, not the terminal

I have a bash script as below which installs zookeeper but only if not installed already. ##zookeper installZook(){ ZOOK_VERSION="3.4.5" ZOOK_TOOL="zookeeper-${ZOOK_VERSION}" ...
0
votes
4answers
117 views

What does “while test $# -gt 0” do?

I'm trying to create a function and believe I found a good working example but I don't understand all the logic behind it. More specifically, on the "while" line, could someone explain what test is ...
3
votes
3answers
129 views

How to call a shell function

How can I use the integer value returned by a function in shell script that takes some arguments as input? I am using the following code: fun() { echo "hello ${1}" return 1 } a= fun 2 echo ...
2
votes
3answers
242 views

Run nvm (bash function) via sudo

I want to write a init script that should basically run nvm use v0.11.12 && forever start /srv/index.js as the user webconfig. nvm is a shell function that is declared in ...
13
votes
5answers
471 views

Stateful bash function

I’d like to implement a function in Bash which increases (and returns) a count with every call. Unfortunately this seems non-trivial since I’m invoking the function inside a subshell and it ...
0
votes
1answer
106 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 ...
1
vote
1answer
153 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 ...
0
votes
1answer
90 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 ...
2
votes
3answers
729 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
3k 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 ] ; ...
5
votes
2answers
185 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 ...
6
votes
1answer
601 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, ...
10
votes
4answers
3k 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 ...
2
votes
2answers
122 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 ...
2
votes
1answer
89 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 ...
4
votes
4answers
2k views

How can I pass a command line argument into a shell script?

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or ...
145
votes
10answers
14k views

In Bash, when to alias, when to script, and when to write a function?

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just ...
4
votes
2answers
270 views

Combine two commands in .bash_profile

In my .bash_profile file, I'd like to setup a single command alias that is two commands that execute one after another. The first command takes an argument from the command line and the second is ...
2
votes
2answers
771 views

creating simple command for sudo apt-get install?

I need to run these commands very often: sudo apt-get install <package> sudo apt-get remove <package> Can I make it simple like: install <package> remove <package> I ...