The function tag has no usage guidance.
3
votes
2answers
114 views
How do I remove all but the file name (with no extension) from a full file path? [duplicate]
I am presently writing a Bash function to convert all the man pages listed by equery files <PACKAGE> | grep /usr/share/man/man (if you are unfamiliar equery is a tool used on Gentoo-based ...
1
vote
3answers
35 views
Bash (Git) - Functions, Alias, and SH script files
I have gotten quite prolific with the use of the aliases, especially with all the different git commands and their order and interdependencies etc. So, I've created a few alias that run more complex ...
4
votes
4answers
185 views
How to create a function that can sort an array in bash?
I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
1
vote
1answer
25 views
Difference between writing `function Name { …; }`, `Name () { …; }` and `function Name () { …; }` in bash [duplicate]
You can write a bash functions several ways:
function JoinStrings {
...;
}
Or
function JoinStrings () {
...;
}
Or
JoinStrings () {
...;
}
Is there any difference between these ...
8
votes
1answer
189 views
Bash alias with a space as a part of the command
I'm trying to create a bash alias, where the alias itself has a space in it.
The idea is that the alias (i.e. con) stands for sudo openvpn --config /path/to/my/openvpn/configs/. Which results in a ...
2
votes
1answer
33 views
How to emulate returning arbitrary values from shell functions?
Shell functions are just statements and they don't return values. Can anyone share best practice on writing functions that return values in bash?
Let's say I've a function that joins two strings:
...
2
votes
1answer
22 views
Function for archiving arbitrary files with encryption
I'm no so advanced in bash so can not make my function work properly.
Here is the code:
archive()
{
for f in $PWD
do
for ((i=1; i++;))
do
7za a "$1".7z $f -pSECRET -mhe
done
...
0
votes
2answers
19 views
Script not able to generate files for backup file
I'm new to shell scripting and I trying to write a script that:
Prompts for a directory
Loops through the directory
If it find files it then backs them up using a function in the script
Creates a ...
0
votes
1answer
33 views
Shell script using function to display system information sending errors
I'm trying to write a script to keep track of my system information. I want to use "function" in the script and just call the functions out. I'm having trouble with the commands working in the ...
2
votes
2answers
100 views
Shell valid function name characters
Using extended Unicode characters is (no-doubt) useful for many users.
Simpler shells (ash (busybox), dash) and ksh do fail with:
tést(){ echo 34; }
tést
But bash, mksh, lksh, and zsh seem to ...
0
votes
2answers
40 views
Shell script using function () not finding command
I'm learning shell scripting and I'm studying how to use function in the shell script. The script is suppose to run as a basic math calculator with two defined numbers by the user. It's suppose to ...
2
votes
1answer
30 views
Is there any danger to using an unset variable in a bash function definition?
Doing some code refactoring, and I realized I don't know if this matters at all:
The function definition is going to be sourced from another file (a sort of library). The function uses certain ...
2
votes
3answers
114 views
Source only part of a script from another script?
This would probably never be the BEST approach to something, but I'm wondering if it's even possible.
Something like:
awk '/function_i_want_to_call/,/^$/{print}' script_containing_function | xargs ...
0
votes
1answer
12 views
Creating a function that reads off an input list of files
I am new to UNIX/Linux and am in the process of writing bioinformatics pipelines.
These pipelines take in input files and pass them through multiple packages.
Say there is a list of files that goes ...
6
votes
1answer
119 views
Function to simplify grep with an often used log
First off, sorry if this is a painfully newbish question!
I'm trying make a function that simplifies grepping a log I have to work with on a regular basis.
I'd like to use extended regexp with it ...
0
votes
2answers
132 views
Bash Scripting echo locally in a function
In bash scripts I try to keep my variables local to functions wherever I can and then pass what I need out of functions like bellow
#!/bin/bash
function FUNCTION() {
local LOCAL="value"
echo ...
1
vote
2answers
48 views
Cursor position in vi at opening of the file
vishex ()
{
echo '#!/bin/bash' > $1;
chmod +x $1;
vi $1
}
The goal of the above function is to have an alias for fast and comfortable creation of bash scripts. I would like that at ...
2
votes
3answers
159 views
Bash function with `getopts` only works the first time it's run
I defined the function f in Bash based on the example here (under "An option with an argument"):
f () {
while getopts ":a:" opt; do
case $opt in
a)
echo "-a was triggered, ...
1
vote
1answer
93 views
bash: syntax error near unexpected token
I'm trying to create a bash alias alias backlight='__backlight () { echo "$@"; cd ~/Code/MSI-Backlight; sudo nodejs ~/Code/MSI-Backlight/msi-backlight.js "$@"; }', it works fine with no parameters but ...
0
votes
2answers
35 views
change permissions to a file and everything inside , recursively
So I want to create an alias called changeAllPermisions that accepts one parameter argument in such a way that when changeAllPermissions argument is called , both Group and Other do not have access to ...
0
votes
0answers
19 views
Lttng tracepoint(provider,tpname, arg)
I want to insert lttng - tracepoint("traceprovider_name"tracepoint_name" "$1"$2") statements with same tracepoint_name and traceprovider_name but with different arguments:
for example:
1)How to ...
0
votes
1answer
57 views
Calling php function from /var/www/folder (as www-user), which is located in a file in /var (or any folder) (with root permissions?)
I wrote a script which "walks" through /var/www/xyz and scans folders and more. The folders are all 'user-bound', so the permissions for a single folder there are set to the specific user.
Running ...
0
votes
2answers
72 views
How to increment local variable in Bash?
Data
1
\begin{document}
3
Code
#!/bin/bash
function getStart {
local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)"
echo $START
}
START2=$(getStart)
echo ...
0
votes
2answers
48 views
Returning local values from Bash variables?
I studied this article called Returning Values from Bash Functions.
Data
Lorem.
\begin{document}
hello
\end{document}
Case #1 which does not work
Code
#!/bin/bash
function getStart {
...
3
votes
1answer
37 views
functions arguments
I am having trouble with what should be a simple bash script.
I have a bash script that works perfectly:
function convert_to ()
x_max=2038
y_max=1146
x_marg=100
y_marg=30
x_grid=150
y_grid=150
if ...
5
votes
4answers
181 views
How to use call-by-reference on an argument in a bash function
I am trying to pass a "var name" to a function, have the function transform the value the variable with such "var name" contains and then be able to reference the transformed object by its original ...
1
vote
2answers
83 views
Returning a variable from a function [closed]
I have the Linux script shown below. I can get it to return from the method decrypt nothing which I need to unzip a file. The method decrypt sends a string with the name of a zip file. Please give ...
2
votes
2answers
153 views
Execute only if it is a bash function
I'm looking for something similar to Bash's built-in command that will only run something if it is a function. So currently I have an insecure way of doing:
# Go through arguments in order
for i in ...
1
vote
2answers
332 views
How to call in a kernel level function in user space [closed]
I've developed some helper functions in the kernel. They're called by other functions in the kernel. Currently, they make my custom kernel panic :(
For now (i.e. debugging), I made them as a passive ...
1
vote
0answers
44 views
Can't get bare-bone simple bash function to work [closed]
I have some tmux scripts in a directory, and I'm trying to make a script to make it easier to call them. Function is below
tms() {
~/linux/scripts/tmux-scripts/$1
}
This is in my bashrc, I then ...
1
vote
4answers
82 views
Problem with entire function in a script
I want to detect online network/shell services in my Solaris.
I write following script for this purpose:
compare_ser()
{
if [ "$1" != "" ]; then
echo "True" >> Solaris.txt
fi
}
export -f ...
1
vote
1answer
77 views
sh - Using Arguments in .profile functions
I want to use an argument in the function I created in my .profile file.
I want to ask for input if no argument is given, otherwise set a variable to $1.
When I check $1 to see if it is empty, I ...
1
vote
3answers
109 views
Using a function defined in a parent script
I wrote the following script in the test.sh:
#!/bin/sh
compare() {
if [ $1 != root ]; then
echo "Fail" >> CAT1.txt
fi
}
awk -F: '$4 == 0' /etc/passwd | cut -d: f1 | xargs -n1 -i bash -c ...
1
vote
1answer
101 views
What is the difference between `autoload` and `autoload -U` in Zsh?
What is the difference between autoload -U and plain autoload?
For instance, here it is recommended to run:
autoload -U run-help
autoload run-help-git
autoload run-help-svn
autoload run-help-svk
...
1
vote
2answers
104 views
Exporting a function from .profile/.bashrc
What am I doing wrong...? It's fine if I do this on the command line and then call it but not when I load it from .profile. Linux Mint Qiana, Bash 4.*, if it matters.
function android() { command ...
2
votes
4answers
262 views
locate alias with find
On small systems where there is no locate installed, How would an alias look like that gets the same result as locate?
I can imagine find can produce the same output so an alias could look like
...
3
votes
2answers
123 views
Whats going on in this function?
log ()
{
A=$1
print "`date '+%m/%d/%y %H:%M:%S'`: $A" >> LOGFILE.txt
print "$A"
}
This is how log function used in most of the shell scripts in our environment.
and its been used in ...
3
votes
4answers
96 views
How can I make source-highlight colorize .dotfiles by default?
Normally when I cat a file like this
it's hard to read without colorizing.
I've managed to get cat to use source-highlight like this:
cdc() {
for fn in "$@"; do
source-highlight ...
5
votes
2answers
133 views
Elegant way to prevent command substitution from removing trailing newline
I'm customizing my zsh PROMPT and calling a function that may or may not echo a string based on the state of an environment variable:
function my_info {
[[ -n "$ENV_VAR"]] && echo "Some ...
5
votes
2answers
491 views
Can you execute a Bash function with the `at` command?
I want to execute a Bash function at a scheduled time. I think the right tool for this is the at command.
However, it doesn't seem to be working.
function stupid () {
date
}
export -f stupid
...
1
vote
1answer
47 views
Aliasing a command with special parameters [duplicate]
I would like to have an alias for the following code:-
g++ *.cc -o * `pkg-config gtkmm-3.0 --cflags --libs`;
but I want that when I enter the alias it should be followed by the file name *.cc and ...
1
vote
2answers
68 views
Accessing fish functions from perl
In bash I can do:
foo() { echo bar; }
export -f foo
perl -e 'system "bash -c foo"'
I can also access the function definition:
perl -e 'print "foo".$ENV{"BASH_FUNC_foo%%"}'
How do I do the same ...
3
votes
2answers
164 views
How to use a bash function like a regular command in a pipe chain?
I'm performing several commands on large files from within a bash script. In order to monitor the progress I use the pv command.
An example command could look like this
cat $IN_FILE | pv -w 20 -s ...
10
votes
1answer
167 views
Track certain parameters on some command
Let's say that I have a command git branch (always with a couple of words) for example.
What I want is to keep track of when this command is executed with arguments. For example, if I execute the ...
4
votes
2answers
174 views
Checking if a command is a built-in in ksh
How can I check if a command is a built-in command for ksh?
In tcsh you can use where; in zsh and bash you can use type -a; and in some modern versions of ksh you can use whence -av.
What I want to ...
1
vote
1answer
87 views
What's the best way to make a bash function in a script as a parameter when running via command line?
I'm fairly new to bash scripting, and wondered what's the simplest way to make bash script functions in a script as the parameter when run via command line?
Example usage:
./myscript function1
...
0
votes
0answers
25 views
Automated function Logging tool
Is there a automated tool for logging function name , function entry and exit parameters per thread for complex multi threaded application.(C / C++)
1
vote
1answer
71 views
Function tracing per thread
I have a complex multi threaded application running on a Cent OS 5.8
the application is coded using C and C++
I am searching for a FUNCTION TRACING tool which can help me do the following.
Trace ...
0
votes
1answer
30 views
Aliasing a command with parameter supplied to another command
Namely: I want to alias tail -f to less +F but let tail with any other parameter supplied work the same way as before.
2
votes
3answers
158 views
What happens if I pass too few parameters to a shell function?
I would like to ask about passing parameters into functions.
I tried this:
function_name $var1 $var2
but usually (sometimes it printed error) it didn't make any difference whether I passed them or ...