I am trying to call a command from a script (defined as functions inside .bashrc) as following
the first function hooks the rm command by deleting any file except a file named "forensicfile1"
#hooking rm command
function rm(){
if [[ $1 = "forensicfile1" ]]; then
echo "$1 file is deleted by user ">forensicaudit
else
rm $1
fi
}
the second hooking function is trying to hook the ls command by listing all files except the forensicfile1
#hooking ls command
function ls(){
if [[ $PWD = "/home/waleed/forensicdata" ]]
then
alias myls='ls "/home/waleed/forensicdata" -1 "forensicfile1" '
myls
else
ls $PWD
fi
}
however the following lines are not working
rm $1 // within the first function
alias myls='ls "/home/waleed/forensicdata" -1 "forensicfile1" '
myls
ls $PWD
can anyone help?
Related
Heres the code:
function rm {
cd ~/
if [[ -d ./Jam/projects/"$1" ]]; then
echo Removing $1 from projects...
rm -r ./Jam/projects/"$1"
elif [[ -d ./Jam/archive/"$1" ]]; then
echo Removing $1 from archives...
rm -r ./Jam/archive/"$1"
else
echo $1 does not exist \in ./Jam/projects/ or ./Jam/archive
exit
fi
echo Finished\!
}
When this is ran, $1 is "Hello World" (a Directory in i./Jam/archive/)
I get this output:
Removing HelloWorld from archives...
-r does not exist in ./Jam/projects/ or ./Jam/archive
Somehow, $1 is assigned to "-r".
I don't know how on earth this would happen. Any help is much appreciated.
Your function is called "rm" and inside your function "rm" you call rm -r thinking it's "normal rm", but it isn't - it's your function, which perfectly demonstrates the danger of calling your function a name that already has a well known meaning.
I want to create a utility function for bash to remove duplicate lines. I am using function
function remove_empty_lines() {
if ! command -v awk &> /dev/null
then
echo '[x] ERR: "awk" command not found'
return
fi
if [[ -z "$1" ]]
then
echo "usage: remove_empty_lines <file-name> [--replace]"
echo
echo "Arguments:"
echo -e "\t--replace\t (Optional) If not passed, the result will be redirected to stdout"
return
fi
if [[ ! -f "$1" ]]
then
echo "[x] ERR: \"$1\" file not found"
return
fi
echo $0
local CMD="awk '!seen[$0]++' $1"
if [[ "$2" = '--reload' ]]
then
CMD+=" > $1"
fi
echo $CMD
}
If I am running the main awk command directly, it is working. But when i execute the same $CMD in the function, I am getting this error
$ remove_empty_lines app.js
/bin/bash
awk '!x[/bin/bash]++' app.js
The original code is broken in several ways:
When used with --reload, it would truncate the output file's contents before awk could ever read those contents (see How can I use a file in a command and redirect output to the same file without truncating it?)
It didn't ever actually run the command, and for the reasons described in BashFAQ #50, storing a shell command in a string is inherently buggy (one can work around some of those issues with eval; BashFAQ #48 describes why doing so introduces security bugs).
It wrote error messages (and other "diagnostic content") to stdout instead of stderr; this means that if your function's output was redirected to a file, you could never see its errors -- they'd end up jumbled into the output.
Error cases were handled with a return even in cases where $? would be zero; this means that return itself would return a zero/successful/truthy status, not revealing to the caller that any error had taken place.
Presumably the reason you were storing your output in CMD was to be able to perform a redirection conditionally, but that can be done other ways: Below, we always create a file descriptor out_fd, but point it to either stdout (when called without --reload), or to a temporary file (if called with --reload); if-and-only-if awk succeeds, we then move the temporary file over the output file, thus replacing it as an atomic operation.
remove_empty_lines() {
local out_fd rc=0 tempfile=
command -v awk &>/dev/null || { echo '[x] ERR: "awk" command not found' >&2; return 1; }
if [[ -z "$1" ]]; then
printf '%b\n' >&2 \
'usage: remove_empty_lines <file-name> [--replace]' \
'' \
'Arguments:' \
'\t--replace\t(Optional) If not passed, the result will be redirected to stdout'
return 1
fi
[[ -f "$1" ]] || { echo "[x] ERR: \"$1\" file not found" >&2; return 1; }
if [ "$2" = --reload ]; then
tempfile=$(mktemp -t "$1.XXXXXX") || return
exec {out_fd}>"$tempfile" || { rc=$?; rm -f "$tempfile"; return "$rc"; }
else
exec {out_fd}>&1
fi
awk '!seen[$0]++' <"$1" >&$out_fd || { rc=$?; rm -f "$tempfile"; return "$rc"; }
exec {out_fd}>&- # close our file descriptor
if [[ $tempfile ]]; then
mv -- "$tempfile" "$1" || return
fi
}
First off the output from your function call is not an error but rather the output of two echo commands (echo $0 and echo $CMD).
And as Charles Duffy has pointed out ... at no point is the function actually running the $CMD.
As for the inclusion of /bin/bash in your function's echo output ... the main problem is the reference to $0; by definition $0 is the name of the running process, which in the case of a function is the shell under which the function is being called. Consider the following when run from a bash command prompt:
$ echo $0
-bash
As you can see from your output this generates /bin/bash in your environment. See this and this for more details.
On a related note, the reference to $0 within double quotes causes the $0 to be evaluated, so this:
local CMD="awk '!seen[$0]++' $1"
becomes
local CMD="awk '!seen[/bin/bash]++' app.js"
I'm thinking what you want is something like:
echo $1 # the name of the file to be processed
local CMD="awk '!seen[\$0]++' $1" # escape the '$' in '$0'
becomes
local CMD="awk '!seen[$0]++' app.js"
That should fix the issues shown in your function's output; as for the other issues ... you're getting a good bit of feedback in the various comments ...
I have a script that checks if there is only one file in a directory. However, I can't figure out how to check if there is only one executable (no file extension) or script (.sh) in that directory. Here's what I currently have:
loc=(/Applications/*)
APPROOTDIR="${loc[RANDOM % ${#loc[#]}]}/"
APPDIR="${APPROOTDIR}Contents/MacOS/"
echo "APPROOTDIR is ${APPROOTDIR}"
echo "APPDIR is ${APPDIR}"
FIAD=$(ls ${APPDIR})
if [ `ls -1 ${APPDIR}* 2>/dev/null | wc -l ` == 1 ]; then
echo "One executable or script: ${FIAD}"
else
echo "Not one executable or script: ${FIAD}"
fi
Does anyone know how I can do this?
Don't parse ls, populate another array with the directory entries and work on it instead.
shopt -s nullglob
# set up loc, APPDIR, etc. here
ent=("$APPDIR"*)
if [[ ${#ent[#]} -eq 1 && ( $ent = *.sh || -x $ent ) ]]; then
echo 'One executable or script: '
else
echo 'Not one executable or script: '
fi
printf '%q\n' "${ent[#]#"$APPDIR"}"
Note that variables with all uppercase names are reserved for shells, it's recommended to use lower or mixed-case variable names.
Say I have searched the path for some files with mdfind. Now I have several addresses printed and what I want to do is cd into the first address. Is there any way writing a shell script or some thing that could facilitate me getting into the path I want without copy and paste?
xxx#MacBook-Pro ~ mdfind -name "adapters.py"
/usr/local/Cellar/python/2.7.10_2/libexec/pip/build/lib/pip/_vendor/requests/adapters.py
/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/adapters.py
/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/adapters.py
/usr/local/Cellar/python/2.7.10_2/libexec/pip/pip/_vendor/requests/adapters.py
/usr/local/lib/python2.7/site-packages/pip/_vendor/requests/adapters.pyc
A quick way would be nesting command substitutions
cd $(dirname $(mdfind -name "adapters.py" | head -n 1))
A function could also define in your .zshrc
func mdcd() {
if [ -z "$1" ]; then return 64; fi
1=$(mdfind -name "$1" | head -n 1)
if [ -z "$1" ]; then return 66; fi
cd $(dirname "$1")
}
I am calling a system call from my linux application.
/* Some file.c */
file.c is embedded in an executable called file.elf. this file.elf is present in directory
/home/ubuntu/file.elf
when i execute the file.elf the echo $BB_PATH prints the executable directory path. i am expecting the directory path to be the path where the script has been placed. i.e
/home/ubuntu/Desktop/BIN/BB/Chk_File.sh
How can this be acheived ?
if(!(system("ls /home/ubuntu/Desktop/BIN/BB")))
{
/* Test Path : remove after testing */
dw_flag = system("/home/ubuntu/Desktop/BIN/BB/Chk_File.sh");//Call to execute Script
dw_flag = WEXITSTATUS(dw_flag);
}
this in turns call the file CHK_File.sh
ret_val=0
BB_PATH=$(pwd)
echo $BB_PATH
if [ ! -f ACTION_TAG.txt ]
then
echo " ACTION_TAG NOT PRESENT "
else
ret_val=1
fi
echo $ret_val
exit $ret_val
You can use dirname to get the directory of the shell script.
echo `dirname $0`
Adding this below lines in script helped me
if [ -L $0 ] ; then
DIR=$(dirname $(readlink -f $0)) ;
else
DIR=$(dirname $0) ;
fi ;
echo $DIR
Now we dont need to worry from which directory the script is called !! Great !!