How to create a directory in a function - bash

Editing:
I'm trying to build a simple function that print out a name, then creates a directory. I'm new at building function in bash, so I have the below script that didn't work:
dest_path=/home/all/todo
line="name"
mkdir_for_name() {
echo $1
mkdir $2
}
mkdir_for_name $name
mkdir_for_name $dest_path/$name
What is wrong with that syntax?

Since you are using echo so I believe you want to print directory name; and off course you need to pass 2 arguments to your function. May be call your function in following way. In this way you need not to change your code.
mkdir_for_name "$name" "$dest_path/$name"
Complete script:
dest_path=/home/all/todo
line="name"
mkdir_for_name() {
echo "$1"
mkdir "$2"
}
mkdir_for_name "$name" "$dest_path/$name"

Your function uses $2, but you're only calling it with one argument, so $2 is empty.

You defined a function that accepts 2 arguments, where the first argument passed is the name to be printed on screen (the "echo" line), while the second argument passed will create the desidered folder.
Passing only one argument will just print on screen that argument

Related

Shell Script msg() echo "${RED}$#${NOCOLOR}", What does it mean [duplicate]

Sometimes I have a one-liner that I am repeating many times for a particular task, but will likely never use again in the exact same form. It includes a file name that I am pasting in from a directory listing. Somewhere in between and creating a bash script I thought maybe I could just create a one-liner function at the command line like:
numresults(){ ls "$1"/RealignerTargetCreator | wc -l }
I've tried a few things like using eval, using numresults=function..., but haven't stumbled on the right syntax, and haven't found anything on the web so far. (Everything coming up is just tutorials on bash functions).
Quoting my answer for a similar question on Ask Ubuntu:
Functions in bash are essentially named compound commands (or code
blocks). From man bash:
Compound Commands
A compound command is one of the following:
...
{ list; }
list is simply executed in the current shell environment. list
must be terminated with a newline or semicolon. This is known
as a group command.
...
Shell Function Definitions
A shell function is an object that is called like a simple command and
executes a compound command with a new set of positional parameters.
... [C]ommand is usually a list of commands between { and }, but
may be any command listed under Compound Commands above.
There's no reason given, it's just the syntax.
Try with a semicolon after wc -l:
numresults(){ ls "$1"/RealignerTargetCreator | wc -l; }
Don't use ls | wc -l as it may give you wrong results if file names have newlines in it. You can use this function instead:
numresults() { find "$1" -mindepth 1 -printf '.' | wc -c; }
You can also count files without find. Using arrays,
numresults () { local files=( "$1"/* ); echo "${#files[#]}"; }
or using positional parameters
numresults () { set -- "$1"/*; echo "$#"; }
To match hidden files as well,
numresults () { local files=( "$1"/* "$1"/.* ); echo $(("${#files[#]}" - 2)); }
numresults () { set -- "$1"/* "$1"/.*; echo $(("$#" - 2)); }
(Subtracting 2 from the result compensates for . and ...)
You can get a
bash: syntax error near unexpected token `('
error if you already have an alias with the same name as the function you're trying to define.
The easiest way maybe is echoing what you want to get back.
function myfunc()
{
local myresult='some value'
echo "$myresult"
}
result=$(myfunc) # or result=`myfunc`
echo $result
Anyway here you can find a good how-to for more advanced purposes

Passing and return value of external function in awk

I am calling function from another script from awk.
Why i need to press enter even after adding /dev/null
Why the passed argument is not displayed. I am getting spaces.
I am not getting the return value from external function.
cat mainpgm.sh
#!/bin/bash
key="09"
awk -v dk="$key" ' { ma = system(". /home/ott/functions.sh;derived dk")</dev/null ; "print returned value" ma } '
cat functions.sh
#!/bin/bash
derived () {
echo "outside function" $dk
return 9
}
If you don't want to process input in awk, redirect its input to /dev/null, and do everything in the BEGIN block. Also, for the dk variable to be replaced with its value, it has to be outside the quotes.
awk -v dk="$key" 'BEGIN {
ma = system(". /home/ott/functions.sh;derived " dk);
print "returned value", ma
}' < /dev/null
To answer your questions:
You put /dev/null in the wrong place. It's supposed to be the input of the script, not the system function.
Two reasons: First, you put dk inside the quotes, so its value is not substituted. Second, the derived function doesn't print its argument, it prints $dk, which is a nonexistent shell variable. The argument to a function is $1, so it should do:
echo "outside function $1"
You had print inside the quotes, so it wasn't being executed as a statement.

Bash: iterate through files based on regex parameter

There are several posts about iterating through bash files like this:
count_files() {
count=0
for f in "filename_*.txt"
do
count=$(($count + 1))
done
echo "Current count:$count"
}
I need to pass in "filename_*.txt" as a param when calling the bash script. Like this:
$: count_files.sh "filename_*.txt"
$: count_files.sh "different_filename_*.txt"
This, however, only gets the first file:
count_files() {
count=0
for f in $1
do
count=$(($count + 1))
done
echo "Current count:$count"
}
How do I pass in a regex param and iterate through it?
NOTE: counting the files is just an example. If you have a simple way to do that, please share, but that's not the main question.
Inside count_files.sh script make sure you call function with quotes like this:
count_files "$1"
instead of:
count_files $1
Later will get you count=1 because wildcard will be expanded before function call to the first file name.

grep text by function parameter in bash

There is a file name as pkg_list
a-1.2b-1.tar.gz
c-2.5b-1.tar.gz
a xx-1.4.txz
a$xx-1.4.txz
中文-3.txz
xx-3.2-2.tar.gz
xxy-1.3.tar.gz
My bash function can input package name like 'xx'
pkg_find() { # <pkg_name> as $1
grep "^$1-[0-9]*" pkg_list
}
pkg_find xx # wish it return xx-3.2-2.tar.gz
I know I can not pass $1 directly into pkg_find, what's the correct method?
[SOLVED]
In this case, because $1 is enclosed by double quote, I found even regex meta chars could pass as parameter.
What you're doing looks right to me.
What isn't working?
I tried the code in your question, and pkg_find xx displays ‘xx-3.2-2.tar.gz’ — which you say is the output you were hoping for.
You can pass $1 directly to pkg_find
pkg_find() { # <pkg_name> as $1
grep "^$1-[0-9]*" pkg_list
}
pkg_find "$1"
In the main body, $1, $2, .. are the script arguments, you get from the command line or another calling script. In a shell function they refer to the function arguments.
When you call this on the command line
sh pkg_find.sh xx
you will get
xx-3.2-2.tar.gz
Your code and your question seem to me to ask for different things, you want to either/both of:
pass a function parameters: Passing parameters to a Bash function
return a string from a function: shell script function return a string
$1, $2 etc at the top-level of a script are the script parameters; within a function they
are set to the function parameters, or unset if there are no parameters.

ksh + print the last value from parameter without print the $parameter

I need advice - how to print the same last value in ksh scripts without to print param argument
so what we can do in ksh inorder to print the last value ?
example - I need to print the last value ( in this case -$ETH_PORT ) , without to define $ETH_PORT parameter after the second echo command
how to print the last value from the last echo/print command?
function test
{
ETH_PORT=eth0
echo $ETH_PORT
# now I need to print the last value ( in this case value from $ETH_PORT param )
echo < what need to write in order to print last value >
}
test
expected output after runing the test function
eth0
eth0
You could subvert echo with a function that kept track of the last thing echo'ed, but that wouldn't capture the last thing to go to stdout:
#!/bin/bash
function echo {
/bin/echo $*
last_echoed="$*"
}
function testit {
ETH_PORT=eth0
echo "this is not echo'ed twice"
echo $ETH_PORT
echo $last_echoed
}
testit
A second option is to use a wrapper script that keeps track of what the last line was and then print it at the end.

Resources