my script does not appear to be working even though I get an exit status of 0
It worked until I made it a function with the
makedrivetree ()
{
}
syntax.
If remove that and keep everthing between the while and done it works fine.
I have it saved in my ~/bin folder as makedrivetree
Here is the full function:
#!/usr/bin/env bash
# a script to write the contents of a directory in tree form and save it to a .txt file
### CONSTANTS
DATE="$(date +%Y%m%d)"
### FUNCTIONS
makedrivetree ()
{
while [ "${*}" != "" ] ; do
#set $DRIVE as the first arugment sent to script
DRIVE="${1}"
#set $OUTPUT
OUTPUT="/Users/$USER/Desktop/$(basename "${DRIVE}")_contents_"${DATE}".txt"
#run tree
tree -I "*.dpx" --si --du -U -o "${OUTPUT}" "${DRIVE}"
#check $? for errors
if [ "$?" -eq "0" ] ; then
echo 'done. moving to next drive.'
else
echo 'error'
fi
shift
done
}
#call the function(s)
makedrivetree
echo 'makedrivetree exited with status' $?
exit
You are not passing any argument to that function. Check out your first line of code in the function
while [ "${*}" != "" ]
what you did there was testing if any argument was passed to the function. When you called the function ( makederivetree ), you did not pass any argument to it . To summarise everything , when you called your function , while loop never runs , because of the condition you specified.
calling the function with makedrivetree "$#" solved the problem.
(Excellent tips/explanations/answers in the comments, as well)
Related
I'm new to bash so assume that I don't understand everything in this simple script as I've been putting this together as of today with no prior experience with bash.
I get this error when I run test.sh:
command substitution: line 29: syntax error near unexpected token `$1,'
./f.sh: command substitution: line 29: `index_of($1, $urls))'
FILE: f.sh
#!/bin/bash
urls=( "example.com" "example2.com")
error_exit()
{
echo "$1" 1>&2
exit 1
}
index_of(){
needle=$1
haystack=$2
for i in "${!haystack[#]}"; do
if [[ "${haystack[$i]}" = "${needle}" ]]; then
echo "${i}"
fi
done
echo -1
}
validate_url_param(){
index=-2 #-2 as flag
if [ $# -eq 0 ]; then
error_exit "No url provided. Exiting"
else
index=$(index_of($1, $urls)) #ERROR POINTS TO THIS LINE
if [ $index -eq -1 ]; then
error_exit "Provided url not found in list. Exiting"
fi
fi
echo $index
}
FILE: test.sh
#!/bin/bash
. ./f.sh
index=$(validate_url_param "example.com")
echo $index
echo "${urls[0]}"
I've lost track of all of the tweaks I tried but google is failing me and I'm sure this is some basic stuff so... thanks in advance.
The immediate error, just like the error message tells you, is that shell functions (just like shell scripts) do not require or accept commas between their arguments or parentheses around the argument list. But there are several changes you could make to improve this code.
Here's a refactored version, with inlined comments.
#!/bin/bash
urls=("example.com" "example2.com")
error_exit()
{
# Include script name in error message; echo all parameters
echo "$0: $#" 1>&2
exit 1
}
# A function can't really accept an array. But it's easy to fix:
# make the first argument the needle, and the rest, the haystack.
# Also, mark variables as local
index_of(){
local needle=$1
shift
local i
for ((i=1; i<=$#; ++i)); do
if [[ "${!i}" = "${needle}" ]]; then
echo "${i}"
# Return when you found it
return 0
fi
done
# Don't echo anything on failure; just return false
return 1
}
validate_url_param(){
# global ${urls[#]} is still a bit of a wart
if [ $# -eq 0 ]; then
error_exit "No url provided. Exiting"
else
if ! index_of "$1" "${urls[#]}"; then
error_exit "Provided url not found in list. Exiting"
fi
fi
}
# Just run the function from within the script itself
validate_url_param "example.com"
echo "${urls[0]}"
Notice how the validate_url_param function doesn't capture the output from the function it is calling. index_of simply prints the result to standard output and that's fine, just let that happen and don't intervene. The exit code tells us whether it succeeded or not.
However, reading the URLs into memory is often not useful or necessary. Perhaps you are simply looking for
grep -Fx example.com urls.txt
I want to create shortcut for 'code file' (VS Code) if there's a second argument, or 'clear' if there's none in a single command line, but i don't know how the bash syntax works.
By looking at the mkcd (mkdir & cd) shortcut that i created:
function mkcd {
if [ ! -n "$1" ]; then
echo "Enter a directory name"
elif [ -d $1 ]; then
echo "\`$1' already exists"
else
mkdir $1 && cd $1
fi
}
I tried to do the same, but the error shows 'syntax error near unexpected token `else''
function c {
if $1 then
code $1
else
clear
fi
}
Your syntax error is that if $1 then is missing a semi-colon (e.g. if $1; then) but read on... There are no "shortcut"s in UNIX. There are scripts, functions, and aliases. It looks like you want to create a function named c to call some command named code when an argument is given, some other command named clear otherwise. That'd be:
c() {
if (( $# > 0 )); then
code "$*"
else
clear
fi
}
I have a bash script which is only meant to used be when sourced.
I want to return from it automatically on any error, similar to what set -e does.
However setting set -e doesn't work for me because it will also exit the users shell.
Right now I'm handling returning manually like this command || return 1, for each command.
You can also use command || true or command || return.
If your requirement is something different, please update more precisely.
You can use trap. E.g.:
// foo.sh
function func() {
trap 'if [ $? -ne 0 ]; then echo "Trapped!"; return ; fi' DEBUG
echo 'foo'
find -name "foo" . 2> /dev/null
echo 'bar'
}
func
Two notes. First, the trap needs to be inside the function as shown. It won't work if it's just inside the script.
Two, there is a significant limitation. Even if you set the return to the trap (e.g., return 1), while func exists after the bad find command, $? is still zero, no matter what. I'm not sure if there's a way around that, so if it's important to preserve the exit value of the failed command, this may not work.
E.g., if you had:
func
func_return=$?
echo "return value is: $func_return"
func_return will always be zero. I've played around with trying to get the exit value of the failed command to pass out of the function trap and into the function exit value, but have not found a way to do it.
If you need to preserve the return value, you could update a global variable inside the debug trap.
If I understand well, you can set -e locally in each function.
cat sourced
f1 () {
local -
set -e
[ "$1" -eq "$1" ] 2> /dev/null && echo "$1"
}
cat script.sh
. sourced
param='bad'
ret=$(f1 "$param")
[ $? -eq 0 ] && echo "result = $ret" || \
echo "error in sourced file with param $param"
param=3
ret=$(f1 "$param")
[ $? -eq 0 ] && echo "result = $ret" || \
echo "error in sourced file with param $param"
How to make a code bellow as a general function to be used entire script in bash:
if [[ $? = 0 ]]; then
echo "success " >> $log
else echo "failed" >> $log
fi
You might write a wrapper for command execution:
function exec_cmd {
$#
if [[ $? = 0 ]]; then
echo "success " >> $log
else
echo "failed" >> $log
fi
}
And then execute commands in your script using the function:
exec_cmd command1 arg1 arg2 ...
exec_cmd command2 arg1 arg2 ...
...
If you don't want to wrap the original calls you could use an explicit call, like the following
function check_success {
if [[ $? = 0 ]]; then
echo "success " >> $log
else echo "failed" >> $log
fi
}
ls && check_success
ls non-existant
check_success
There's no really clean way to do that. This is clean and might be good enough?
PS4='($?)[$LINENO]'
exec 2>>"$log"
That will show every command run in the log, and each entry will start with the exit code of the previous command...
You could put this in .bashrc and call it whenever
function log_status { [ $? == 0 ] && echo success>>/tmp/status || echo fail>>/tmp/status }
If you want it after every command you could make the prompt write to the log (note the original PS1 value is appended).
export PS1="\$([ \$? == 0 ] && echo success>>/tmp/status || echo fail>>/tmp/status)$PS1"
(I'm not experienced with this, perhaps PROMPT_COMMAND is a more appropriate place to put it)
Or even get more fancy and see the result with colours.
I guess you could also play with getting the last executed command:
How do I get "previous executed command" in a bash script?
Get name of last run program in Bash
BASH: echoing the last command run
I use "source" inside a bash script, as follows:
#!/bin/bash
source someneatscriptthatendsprematurely.sh
I would like to exit from the someneatscriptthatendsprematurely.sh script, without exiting from the main script.
Any help appreciated!
You need the return statement:
return [n]
Causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command executed in the function body. If used outside a function, but during execution of a script by the . (source) command, it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by ., the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script.
You can see this in action with the following two scripts:
script1.sh:
. script2.sh
echo hello again
script2.sh:
echo hello
return
echo goodbye
When you run script1.sh, you see:
hello
hello again
Is it important that you can change environment variables? Since otherwise you can just execute the script by executing it without source:
someneatscriptthatendsprematurely.sh
I had the same problem just now
I realized that adding a checker function and returning that will not also return the function on its caller for example.
On bash_functions
function install_packer_linux() {
check_wget && check_unzip
wget https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip
unzip packer_1.1.2_linux_amd64.zip
mv packer ~/.local/bin
rm -f packer_1.1.2_linux_amd64.zip
}
function check_unzip() {
if ! [ -x "$(command -v unzip)" ]; then
echo "Error: unzip is not installed"
return 1
else
return 0
fi
}
function check_wget() {
if ! [ -x "$(command -v wget)" ]; then
echo "Error!: wget is not installed"
return 1
else
return 0
fi
}
$ source ~/.bash_functions
What happens here is since the checkers is the only place its returned so install_packer_linux will still continue
So you can do two things here. Either keep the current format (function calling another function) as is and evaluate using truthy value then return if the values are not truthy or rewrite the checker on the main installer_packer_linux function
Truthy:
function install_packer_linux() {
check_wget && check_unzip || return
wget https://releases.hashicorp.com/packer/1.1.2/packer_1.1.2_linux_amd64.zip
unzip packer_1.1.2_linux_amd64.zip
mv packer ~/.local/bin
rm -f packer_1.1.2_linux_amd64.zip
}
Notice we added || return after the checks and concatenated the checks using &&
so if not both checks are truthy we return the function