Mkdir fails when defined and called with functions.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Mkdir fails when defined and called with functions.
# 1  
Old 12-14-2016
Hammer & Screwdriver Mkdir fails when defined and called with functions.

In the below script i found that the alias mkdir_s is getting invoked from function configure() i.e the alias is kicking in.

Code:
#!/bin/bash -e
shopt -s expand_aliases

alias mkdir=mkdir_s
mkdir_s(){
if [[ -d $1 ]]; then
return
else
/usr/bin/mkdir "$1"
return
fi
}

configure()
{
mkdir -p /tmp/logs
}

configure

Inside the mkdir_s function it is also able to check the condition if the directory does not exist and executes the mkdir command. but then, I get the following error as the output.

Quote:
mkdir: usage: mkdir [-m mode] [-p] dirname ...

Can you please explain why and what is the fix ?
# 2  
Old 12-14-2016
This is because in this case $1 equals -p (and $2 equals /tmp/logs, but that is not being used inside the function)
# 3  
Old 12-14-2016
Quote:
Originally Posted by Scrutinizer
This is because in this case $1 equals -p (and $2 equals /tmp/logs, but that is not being used inside the function)
So, how can we optimize the function mkdir_s so as to understand the difference between mkdir dir and mkdir -p dir and act accordingly.

This is what i think will take care of the problem.

Code:
mkdir_s(){
var1=$1
if [[ $var1 == -* ]]; then var2=$2; else var2=var1; fi
if [[ -d $var2 ]]; then
#echo " directory EXISTS "
return
else
#echo " Creating DIRECTORY"
/usr/bin/mkdir "$var2"
return
fi
}

Let me know if you have other suggestions.

Last edited by mohtashims; 12-14-2016 at 03:32 PM..
# 4  
Old 12-14-2016
Quote:
Originally Posted by mohtashims
So, how can we optimize the function mkdir_s so as to understand the difference between mkdir dir and mkdir -p dir and act accordingly.
What are you going to do about mkdir's other options? Pehaps someone wants a dir created with a specific mode via -m, or has to use your script under SElinux and needs -Z.

Are you going to completely re-implement mkdir feature-by-feature?

What exactly is your goal here in intercepting mkdir?
# 5  
Old 12-14-2016
Tools

Quote:
Originally Posted by Corona688
What are you going to do about mkdir's other options? Pehaps someone wants a dir created with a specific mode via -m, or has to use your script under SElinux and needs -Z.

Are you going to completely re-implement mkdir feature-by-feature?

What exactly is your goal here in intercepting mkdir?
Good Question.

The only reason i m doing this is To avoid mkdir to error out [avoid exit 1 code i.e handle it] incase the directory already exists. Thats the only purpose of my efforts.
# 6  
Old 12-14-2016
Quote:
Originally Posted by mohtashims
Good Question.

The only reason i m doing this is To avoid mkdir to error out [avoid exit 1 code i.e handle it] incase the directory already exists. Thats the only purpose of my efforts.
Then just use mkdir -p, which will not fail when directory already exists:

Code:
$ mkdir -p /tmp && echo "I did not fail"
I did not fail

$

# 7  
Old 12-14-2016
mkdir -p won't error out if a directory branch exists.
You can
- simply ignore an error exit code of mkdir.
- use getopts in your function.
- use a compiler to rewrite the mkdir command.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[solved] awk: placement of user-defined functions

Hi folks, is there any recommendation, especially from a point of performance, about where to place a user-defined function in awk, like in BEGIN{} or if it is only need once at the end in END{}? Or doesn't it matter at all since, awk is so clever and only interprets it once, wherever it is... (3 Replies)
Discussion started by: zaxxon
3 Replies

2. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

3. Shell Programming and Scripting

User defined functions in awk

Hi; Is der ne to to use user defined functions for the values in awk find $1 -type f -ls | nawk '{{print "|"$3"|"$5"|"$6"|"$8"|"$9"|"$10"|"} for(i=11;i<=NF;i++){printf("%s",$i)}}' In above command i want to append some values returned by user functions on line. thnks; ajay (1 Reply)
Discussion started by: ajaypadvi
1 Replies

4. Shell Programming and Scripting

Perl script to find where functions is called in c

Hello, I need to write a perl script to find where functions is called in c files.. The script should scan the file and find out the function names and then search to see where they are called... Lets for now assume all functions are internal. I don't know where to start :( ... (4 Replies)
Discussion started by: bojomojo
4 Replies

5. Emergency UNIX and Linux Support

Functions defined in header / cpp file behaves different

File: A.h class A { public: struct x X; int show() { x.member_variable ? 0: -1; } }; Now if A.cpp is complied which includes A.h (which is actually in a huge project space) we see that x.member_variable value is not as expected. But if remove the show() method and place... (4 Replies)
Discussion started by: uunniixx
4 Replies

6. UNIX for Dummies Questions & Answers

Expect "interact" fails when called from another script

So, I have an expect script (let's call it expect.exp) that takes 3 arguments. It logs into a remote server, runs a set of commands, then hands control over to the user by the "interact" command. If I call this script from the command line, it works properly. Now I'd like to apply this script... (2 Replies)
Discussion started by: treesloth
2 Replies

7. Shell Programming and Scripting

user-defined functions, "$@", $jobs and $ps wwaxu

Imagine a user-defined function. func() { /usr/pkg/bin/program long-string-of-switches-and-configs "$@" ;} I execute it once. Then background it. I execute another instance. Then bg it. func unique-user-input ^Z func unique-user-input ^Z First I view with ps ps wwaux ... (0 Replies)
Discussion started by: uiop44
0 Replies

8. Shell Programming and Scripting

Shell Script to display function names (called & defined) in a C++ Source Code

Hello to all, I am looking for a way to display only the names of function (calls & definition) of a C++ source code.There is already a post related to this, but the script is to find the functions using a specific variable, and the replies are not that convincing since they cannot be used for... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. UNIX for Dummies Questions & Answers

finding called functions recursively

I want to write a shell script which traverses a cpp file. Suppose there is function fncn_name6 .. which is called by fncn_name5 which in turn called by fncn_name4 and so on .. in a single cpp class. ie fncn_name1 { fncn_name2 { fncn_name3 } { fncn_name4 } } so fncn_name1 is... (2 Replies)
Discussion started by: ultimatix
2 Replies

10. Windows & DOS: Issues & Discussions

Samba (SMB) client fails: "Called name not present"

Hi, I issue smbclient on a Linux REd hat server : smbclient -L ***.16.0.42 -U domaine/Administrator Password: Domain= OS= Server= Domain= OS= Server= Sharename Type Comment --------- ---- ------- IPC$ IPC IPC distant ... (0 Replies)
Discussion started by: big123456
0 Replies
Login or Register to Ask a Question