Script error in pre-commit-msg with husky/commitzen - pre-commit-hook

I'm trying to run the following in prepare-commmit-msg for husky:
(exec < /dev/tty & node_modules/.bin/cz --hook) || true < / dev/null
But I'm getting an error that the true argument after there serves nothing.

Related

Any idea how i can use timeout command with condition in linux and shell script

aim working on some shell script , i would like to check the following command
df -h
if timeout more than 60 second then echo "error " else pass.
so if this command will not responding for 1 minute then I will receive and error message , otherwise it will pass , any creative idea will be high apricated .
timeout 60 df -h > /dev/null && echo "PASS" || echo "PROBLEM"
Using timeout, if the command df -h doesn't execute within 60 seconds or errors, it will return a none 0 return code. If a none 0 return code is returned echo "PROBLEM" using the || condition.

I keep receiving this error message: del: line 13: syntax error: unexpected end of file

I keep receiving this error message:
del: line 13: syntax error: unexpected end of file
This is my script:
1 echo -e "Enter a filename"
2 read filename
3 if [$filename = myfirst]
4 then
5 echo -e "do you want to delete?"
6 read answer
7 if [answer= Y]
8 then rm myfirst
9 else [answer = N]
10 echo -e "file not deleted"
11 fi
12 exit0
Your if/fi syntax is not complete or closed (missing fi).
To detect such bugs prior to running your script you should always use
bash -n scriptname
This performs a syntax check detecting such problems without actually running the script.

bash function return status

I have a function
function f() {
command 1
command 2
command 3
command 4
}
I want function f() to somehow tell me there is an error if any of the 4 commands fails.
I also don't want to set -e. I want four commands all run, even if one fails.
How do I do that? Sorry for the newbie question -- I am quite new to bash scripting.
If I understand what you're asking correctly, you can do this:
f() {
err=""
command 1 || err=${err}1
command 2 || err=${err}2
command 3 || err=${err}3
command 4 || err=${err}4
# do something with $err to report the error
}
Of course, instead of using a variable you could simply put echo commands after the || if all you need to do is print an error message:
f() {
command 1 || echo "command 1 failed" >&2
command 2 || echo "command 2 failed" >&2
command 3 || echo "command 3 failed" >&2
command 4 || echo "command 4 failed" >&2
}
Take advantage of "$#" and write a higher-order function:
function warner () { "$#" || echo "Error when executing '$#'" >&2; }
Then:
warner command 1
warner command 2
warner command 3
warner command 4
Test:
$ warner git status
fatal: Not a git repository (or any of the parent directories): .git
Error when executing 'git status'
$ warner true
As #user1261959 found out, this is the same approach as in this answer.
You can check the exit flag of any of your commands after they run by checking the variable $?.If it returns 0 usually everything went well otherwise it means an error occurred. You can make your function return a flag to the caller by using the keyword return

Exit not working inside function in SSH's script

I'm executing a local script, which executes a second script via SSH:
RES=$(ssh user#destination 'bash -s 2>&1' < remoteScript.sh)
I need the second script (remoteScript.sh) to exit with code 1 when I detect an error, which is why I built a function which is executed after some delicate calculations:
ErrorCheck()
{
if [ ! "$?" = "0" ]
then
exit 1
fi
}
Unfortunately, the exit 1 is behaving as a return instruction, as the remoteScript.sh execution continues. According to my testing this is related to using sudo su - {user} at the beginning of the remote script.
What am I doing wrong here? How can I make that function, end the remoteScript.sh?
EDIT:
I'm adding a very limited example of my remote script. I've tried running it locally and it works. It just fails when running via SSH.
sudo su - {user}
DelicateFunction()
{
# The following instruction fails
thisisanerror123
ErrorCheck
}
ErrorCheck()
{
if [ ! "$?" = "0" ]
then
exit 1
fi
}
printf "Executing DelicateFunction\n"
DelicateFunction
printf "I should not print!"
The only way that won't exit is if it's running in a sub-shell.
exit is guaranteed to exit its shell.
Incidentally, you can write ErrorCheck a lot simpler:
ErrorCheck()
{
[ $? = 0 ] || exit 1
}

How to get the exit code of spawned process in expect shell script?

I am trying to execute a script that executes an EXPECT script and a spawned process which has exit code in it. But I'm unable to get the exit code of the spawned process to main script. I'm always getting zero as success.
expect script is :
[Linux Dev:anr ]$ cat testexit.sh
#!/bin/bash
export tmp_script_file="/home/anr/tmp_script_temp.sh"
cp /home/anr/tmp_script $tmp_script_file
chmod a+x $tmp_script_file
cat $tmp_script_file
expect << 'EOF'
set timeout -1
spawn $env(tmp_script_file)
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
}
EOF
echo "spawned process status" $?
rm -f $tmp_script_file
echo "done"
Spawned script:
[Linux Dev:anr ]$ cat tmp_script
exit 3
Execution of Expect script:
[Linux Dev:anr ]$ ./testexit.sh
exit 3
spawn /home/anr/tmp_script_temp.sh
spawned process status 0
done
Problem is I am unable to get the spawned exit return code to expect script. I want the exit code 3 of spawned script to main script and main script should be exit with exit code 3.
Please help me to get the spawned exit code to main script.
You get the exit status of the spawned process with the wait command:
expect <<'END'
log_user 0
spawn sh -c {echo hello; exit 42}
expect eof
puts $expect_out(buffer)
lassign [wait] pid spawnid os_error_flag value
if {$os_error_flag == 0} {
puts "exit status: $value"
} else {
puts "errno: $value"
}
END
hello
exit status: 42
From the expect man page
wait [args]
delays until a spawned process (or the current process if none is named) terminates.
wait normally returns a list of four integers. The first integer is the pid of the process that was waited upon. The second integer is the corresponding spawn id. The third integer is -1 if an operating system error occurred, or 0 otherwise. If the third integer was 0, the fourth integer is the status returned by the spawned process. If the third integer was -1, the fourth integer is the value of errno set by the operating system. The global variable errorCode is also set.
Change
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
}
to
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
eof
}
Then add the lassign and if commands.
With the help of glenn, I got solution.. and my final script is::
expect script is
[Linux Dev:anr ]$ cat testexit.sh
#!/bin/bash
export tmp_script_file="/home/anr/tmp_script_temp.sh"
cp /home/anr/tmp_script $tmp_script_file
chmod a+x $tmp_script_file
cat $tmp_script_file
expect << 'EOF'
set timeout -1
spawn $env(tmp_script_file)
expect {
"INVALID " { exit 4 }
timeout { exit 4 }
eof
}
foreach {pid spawnid os_error_flag value} [wait] break
if {$os_error_flag == 0} {
puts "exit status: $value"
exit $value
} else {
puts "errno: $value"
exit $value
}
EOF
echo "spawned process status" $?
rm -f $tmp_script_file
echo "done"
Spawned script:
[Linux Dev:anr ]$ cat tmp_script
exit 3
Execution of Expect script:
[Linux Dev:anr ]$ ./testexit.sh
exit 3
spawn /home/anr/tmp_script_temp.sh
exit status: 3
spawned process status 3
done
Thanks Glenn once again..
After struggling few days with expanding variable inside the expect heredoc, finally i came across an another approach i thought may be helpful to someone in need. My requirement was to pass command and password to a shell function, execute the command in remote host as part of expect heredoc and get the return exit code.
Example:
function shell_function {
# Get the command and password as arguments
# Run command using expect
# Return the exit code
}
shell_function <cmd> <password>
echo $?
Like everyone else expanding of variable inside the heredoc was a problem, which required exporting the value into an environment variable and use env to get the variable inside heredoc. Since, password was one of the arguments i didn't want to store it as part of an environment variable. So, instead of enclosing heredoc opening with single quotes, the variables of heredoc have been escaped. This allowed the direct usage of arguments passed.
Following is the final script:
#! /bin/bash
# This function runs a command like 'ssh' and provides the password
function run_with_password {
cmd="$2"
paswd="$1"
expect << END
set timeout 60
spawn $cmd
expect {
"yes/no" { send "yes\r" }
"*assword*" { send -- $paswd\r }
}
expect EOF
catch wait result
exit [lindex \$result 3]
END
}
my_password="AnswerIS42Really?"
cmd_to_run="ssh userid#hostname"
cmd_to_run="$cmd_to_run ls .sawfish"
run_with_password $my_password "$cmd_to_run"
echo "Command run code: $?"
In the above code the escaped expect variable is $result. After changing the variable to \$result, the script started working like charm.
My sincere thanks to users who have provided answers to following questions, which served as a stepping stones to reach my solution.
Douglas Leeder: help with expect script, run cat on remote comp and get output of it to the variable
glenn jackman: How to return spawned process exit code in Expect script?

Resources