Curl command in bash script error - bash

I'm trying to run a curl command within a bash script and I'm coming across the following error:
curl: (26) failed creating formpost data
When I echo the curl command and run that from the command line, that has no problems running. I've ensured there are read permissions for everyone on the file and the folder it's in, as well as using relative and absolute paths.
Here's a sample of the script:
#!/bin/bash
usage(){
echo "usage: <command> options: <u|p|f> "
}
while getopts u:p:f: option
do
case "${option}"
in
u) USERNAME=${OPTARG};;
p) PORT=${OPTARG};;
f) FILE=$OPTARG;;
esac
done
if [ $OPTIND -eq 1 ]; then
#echo "No options were passed";
usage
exit
fi
echo "Please enter your password: "
read PASSWORD
TEST="curl -u $USERNAME:$PASSWORD -F file=#\"$FILE\" -F force=true -F install=true http://localhost:$PORT/test.jsp"
echo $TEST
$TEST
echo $?
Thanks!

Related

Spring batch Shell Script pass status

I want to know how spring batch send job status (completed or failed) to shell script for that he launches his treatment.
Thank you.
I have an example which is to capture an exit code from spring batch called from Shell script.
JOB_RET=$?.
Hope it helps.
#!/bin/bash
#
# Launch Spring Batch
#
#Get the absolute path to the folder containing the folder this script is located in
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
HOME_DIR=$(dirname $BIN_DIR)
BATCH_HOME=$HOME_DIR
RUN_ID=$(date +"%Y-%m-%d %H:%M:%S")
LOG4J_CONF=file:$HOME_DIR/config/log4j.xml
function show_help {
echo "usage: $BASH_SOURCE -o <options> -d <month> -y <year>"
echo " where command is one of the following: "
echo " options - Options (such as autoLogin|autoLogOut)."
echo " month - Enter a month as Job param."
echo " year - Enter a year as Job param."
exit 1
}
# Read command line options
OPTIND=1 # Reset in case getopts has been used previously in the shell.
while getopts "ho:d:y:" opt; do
case "$opt" in
h)
show_help
exit 0
;;
d) options=$OPTARG
;;
s) month=$OPTARG
;;
o) year=$OPTARG
;;
esac
done
JOB_PARAMS="options=$options month=$month year=$year"
#Run the job
$JAVA_HOME -cp "$(echo $HOME_DIR/lib/*.jar | tr ' ' ':') \
-Djava.security.properties==$JAVA_SECURITY_FILE \
-Dlog4j.configuration=$LOG4J_CONF \
-DBATCH_HOME=$HOME_DIR -d64 \
org.springframework.batch.core.launch.support.CommandLineJobRunner batch-jobs.xml <job-name> run.id="$RUN_ID" ${JOB_PARAMS} > $HOME_DIR/logs/stdout.log 2>&1
JOB_RET=$?
echo "Job returns $JOB_RET" >> $HOME_DIR/logs/stdout.log
exit $JOB_RET
https://bigzidane.wordpress.com/2016/06/26/launch-spring-batch-job-from-shell-script-sh/
The ExitCodeMapper is what you are looking for. It maps the exit code of your job to an integer which will be the exit code of the JVM running your job.
If you run your job from a shell script, this exit code will be the value returned by your script.

Delete first line/chars on Windows Pipe

I'm trying to capture a live tcpdump from a custom designed linux system. The command I'm using so far is:
plink.exe -ssh user#IP -pw PW "shell nstcpdump.sh -s0 -U -w - not port 22 and not host 127.0.0.1" | "C:\Program Files\Wireshark\wireshark" -i -
This will fail since when executing the command on the remote system, this (custom) shell will output "Done" before sending data. I tried to find out a way to remove the "Done" message from the shell but doesn't appear to be any.
So I came up with this (added findstr -V):
plink.exe -ssh user#IP -pw PW "shell nstcpdump.sh -s0 -U -w - not port 22 and not host 127.0.0.1" | findstr -V "Done" | "C:\Program Files\Wireshark\wireshark" -i -
This works more or less fine since I will get some errors and the live capture will stop. I believe it might have something to do with the buffers, but I'm not sure.
Does anyone know of any other method of removing/bypassing the first bytes/chars of the output from plink/remote shell?
[edit]
as asked, nstcpdump.sh is a wrapper for tcpdump. as mentioned before, this system is highly customized. nstcpdump.sh code:
root#hostname# cat /netscaler/nstcpdump.sh
#!/bin/sh
# piping the packet trace to the tcpdump
#
# FILE: $Id: //depot/main/rs_120_56_14_RTM/usr.src/netscaler/scripts/nstcpdump.sh#1 $
# LAST CHECKIN: $Author: build $
# $DateTime: 2017/11/30 02:14:38 $
#
#
# Options: any TCPDUMP options
#
TCPDUMP_PIPE=/var/tmp/tcpdump_pipe
NETSCALER=${NETSCALER:-`cat /var/run/.NETSCALER`}
[ -r ${NETSCALER}/netscaler.conf ] && . ${NETSCALER}/netscaler.conf
TIME=${TIME:-3600}
MODE=${MODE:-6}
STARTCMD="start nstrace -size 0 -traceformat PCAP -merge ONTHEFLY -filetype PIPE -skipLocalSSH ENABLED"
STOPCMD="stop nstrace "
SHOWCMD="show nstrace "
NSCLI_FILE_EXEC=/netscaler/nscli
NSTRACE_OUT_FILE=/tmp/nstrace.out
NS_STARTTRACE_PIDFILE=/tmp/nstcpdump.pid
TRACESTATE=$(nsapimgr -d allvariables | grep tracestate | awk '{ print $2}')
trap nstcpdump_exit 1 2 15
nstcpdump_init()
{
echo "##### WARNING #####"
echo "This command has been deprecated."
echo "Please use 'start nstrace...' command from CLI to capture nstrace."
echo "trace will now start with all default options"
echo "###################"
if [ ! -d $NSTRACE_DIR ]
then
echo "$NSTRACE_DIR directory doesn't exist."
echo "Possible reason: partition is not mounted."
echo "Check partitions using mount program and try again."
exit 1
fi
if [ ! -x $NSCLI_FILE_EXEC ]
then
echo "$NSCLI_FILE_EXEC binary doesn't exist"
exit 1
fi
if [ -e $NSTRACE_OUT_FILE ]
then
rm $NSTRACE_OUT_FILE
echo "" >> $NSTRACE_OUT_FILE
fi
}
nstcpdump_start_petrace()
{
sleep 0.5;
$NSCLI_FILE_EXEC -U %%:.:. $STARTCMD >/tmp/nstcpdump.sh.out
rm -f ${NS_STARTTRACE_PIDFILE}
}
nstcpdump_start()
{
# exit if trace is already running
if [ $TRACESTATE -ne 0 ]
then
echo "Error: one instance of nstrace is already running"
exit 2
fi
nstcpdump_start_petrace &
echo $! > ${NS_STARTTRACE_PIDFILE}
tcpdump -n -r - $TCPDUMPOPTIONS < ${TCPDUMP_PIPE}
nstcpdump_exit
exit 1
}
nstcpdump_exit()
{
if [ -f ${NS_STARTTRACE_PIDFILE} ]
then
kill `cat ${NS_STARTTRACE_PIDFILE}`
rm ${NS_STARTTRACE_PIDFILE}
fi
$NSCLI_FILE_EXEC -U %%:.:. $STOPCMD >> /dev/null
exit 1
}
nstcpdump_usage()
{
echo `basename $0`: utility to view/save/sniff LIVE packet capture on NETSCALER box
tcpdump -h
echo
echo NOTE: tcpdump options -i, -r and -F are NOT SUPPORTED by this utility
exit 0
}
########################################################################
while [ $# -gt 0 ]
do
case "$1" in
-h )
nstcpdump_usage
;;
-i )
nstcpdump_usage
;;
-r )
nstcpdump_usage
;;
-F )
nstcpdump_usage
;;
esac
break;
done
TCPDUMPOPTIONS="$#"
check_ns nstcpdump
#nstcpdump_init
#set -e
if [ ! -e ${TCPDUMP_PIPE} ]
then
mkfifo $TCPDUMP_PIPE
if [ $? -ne 0 ]
then
echo "Failed creating pipe [$TCPDUMP_PIPE]"
exit 1;
fi
fi
nstcpdump_start
Regards

Bash sub script redirects input to /dev/null mistakenly

I'm working on a script to automate the creation of a .gitconfig file.
This is my main script that calls a function which in turn execute another file.
dotfile.sh
COMMAND_NAME=$1
shift
ARG_NAME=$#
set +a
fail() {
echo "";
printf "\r[${RED}FAIL${RESET}] $1\n";
echo "";
exit 1;
}
set -a
sub_setup() {
info "This may overwrite existing files in your computer. Are you sure? (y/n)";
read -p "" -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
for ARG in $ARG_NAME; do
local SCRIPT="~/dotfiles/setup/${ARG}.sh";
[ -f "$SCRIPT" ] && echo "Applying '$ARG'" && . "$SCRIPT" || fail "Unable to find script '$ARG'";
done;
fi;
}
case $COMMAND_NAME in
"" | "-h" | "--help")
sub_help;
;;
*)
CMD=${COMMAND_NAME/*-/}
sub_${CMD} $ARG_NAME 2> /dev/null;
if [ $? = 127 ]; then
fail "'$CMD' is not a known command or has errors.";
fi;
;;
esac;
git.sh
git_config() {
if [ ! -f "~/dotfiles/git/gitconfig_template" ]; then
fail "No gitconfig_template file found in ~/dotfiles/git/";
elif [ -f "~/dotfiles/.gitconfig" ]; then
fail ".gitconfig already exists. Delete the file and retry.";
else
echo "Setting up .gitconfig";
GIT_CREDENTIAL="cache"
[ "$(uname -s)" == "Darwin" ] && GIT_CREDENTIAL="osxkeychain";
user " - What is your GitHub author name?";
read -e GIT_AUTHORNAME;
user " - What is your GitHub author email?";
read -e GIT_AUTHOREMAIL;
user " - What is your GitHub username?";
read -e GIT_USERNAME;
if sed -e "s/AUTHORNAME/$GIT_AUTHORNAME/g" \
-e "s/AUTHOREMAIL/$GIT_AUTHOREMAIL/g" \
-e "s/USERNAME/$GIT_USERNAME/g" \
-e "s/GIT_CREDENTIAL_HELPER/$GIT_CREDENTIAL/g" \
"~/dotfiles/git/gitconfig_template" > "~/dotfiles/.gitconfig"; then
success ".gitconfig has been setup";
else
fail ".gitconfig has not been setup";
fi;
fi;
}
git_config
In the console
$ ./dotfile.sh --setup git
[ ?? ] This may overwrite existing files in your computer. Are you sure? (y/n)
y
Applying 'git'
Setting up .gitconfig
[ .. ] - What is your GitHub author name?
Then I cannot see what I'm typing...
At the bottom of dotfile.sh, I redirect any error that occurs during my function call to /dev/null. But I should normally see what I'm typing. If I remove 2> /dev/null from this line sub_${CMD} $ARG_NAME 2> /dev/null;, it works!! But I don't understand why.
I need this line to prevent my script to echo an error in case my command doesn't exists. I only want my own message.
e.g.
$ ./dotfile --blahblah
./dotfiles: line 153: sub_blahblah: command not found
[FAIL] 'blahblah' is not a known command or has errors
I really don't understand why the input in my sub script is redirected to /dev/null as I mentioned only stderr to be redirected to /dev/null.
Thanks
Do you need the -e option in your read statements?
I did a quick test in an interactive shell. The following command does not echo characters :
read -e TEST 2>/dev/null
The following does echo the characters
read TEST 2>/dev/null

bash script - unable to evaluate variable to string correctly

Trying to debug my bash script. What's wrong with my syntax here? I'm trying to evaluate a parameter entered by the user and based on that run one of my IF-THEN statements. However, I'm getting a command not found.
Here's my script thus far:
if [[ $# != 4 ]]; then
echo "Usage: ./test.sh <ABC|XYZ> <owner> <db> <TARGETHOST>" 2>&1
exit 1
fi
case $1 in
ABC|XYZ)
filename="get-$1.sql"
;;
*)echo "Must enter ABC or XYZ"
exit 1
;;
esac
export OWNER=$2
export DB=$3
export HOST_NM=$4
export PORT=5432
export LOG="test-$1.log"
PSQL=`which psql`
if [[$1=="ABC"]]; then
RUNCLI=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -F $'\t' --no-align -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
if [[$1=="XYZ"]]; then
RUNCLI2=$("$PSQL" -h $HOST_NM -p $PORT -U $OWNER $DB -a -f get-$1.sql | tee >> $LOG)
exit 1
else
echo "Error running report ..."
fi
Error:
./test.sh: line 41: [[XYZ==ABC]]: command not found
Error running report ...
./test.sh: line 51: [[XYZ==XYZ]]: command not found
Error running report ...
Although the question is already answered in the comment section I want to give an answer and share some knowledge which is not obvious (at least it was not for me).
The if in bash just checks the return code of the following command, which means that instead of if [ condition ]... or if [[ condition ]]... you could also write if ./configure && make....
[ and [[ are commands or shell-built-ins, respectively, as well and not part of the if syntax. An which [ for instance returns /bin/[.
At this point it is obvious that you need spaces between the brackets and the condition since it is just just a set of parameters passed to a command.
If you have this in mind, you will never forget the spaces again.

Collecting data from a UNIX program with Bash Redirection

I am attempting to write a script that outputs a CSV file (specified by the user), from the temperature value given by the acpi program. Each data entry is appended to the CSV file, every second (for now).
I have two issues that need to be resolved in my script:
To fix the errors in the script (such as the file access error on line 14), and to allow the user to specify the time delay.
Here's the bash script:
#!/bin/bash
# This script monitors the CPU temperature (in degrees C) over time, and
# records the data to a CSV file.
# Written by Ben Cottrell
function start_log()
{
touch "$output_csv"
if hash acpi 2>/dev/null; then
echo -e "time,temperature\n" > "$output_csv"
while (true); do
sleep 1
echo -e "$(date +%T),$(acpi -t | awk '{print $4}')\n" >> "$output_csv"
done
else
echo "Error: acpi is not installed. Please execute \"sudo apt-get install acpi, before running this script.\""
fi
}
if [ "$1" ]; then
local output_csv = $1
start_log
else
echo -e "Error: No filename specified.\n"
fi
echo -e "\n"
Here's a rewrite with the worst errors and idiom violations fixed.
#!/bin/bash
function start_log()
{
local output_csv=$1
local delay=$2
if hash acpi 2>/dev/null; then
echo "time,temperature" > "$output_csv"
while true; do
sleep "$delay"
echo "$(date +%T),$(acpi -t | awk '{print $4}')"
done >>"$output_csv"
else
cat <<________error >&2
$0: Error: acpi is not installed.
Please execute \"sudo apt-get install acpi\" before running this script.
________error
exit 2
fi
}
delay=1 # default to 1 sec delay
while true; do
case $1 in
-t) shift; delay=$1; shift;; # Maybe validate the value of $delay
-*) echo "$0: Unknown option: '$1'" >&2; exit 1;;
*) break;;
esac
done
if [ "$1" ]; then
start_log "$1" "$delay"
else
echo "$0: Error: No filename specified." >&2
exit 1
fi
Including the program name in error messages is useful when it is being invoked from another script, which might get invoked from another script, etc.
Notice how the redirection is only done once, after the loop, instead of repeatedly inside the main loop.
Notice also how the function receives its parameters from the caller, instead of pulling in global variables.
Hand-crafting the option parsing is hardly more complex than doing it "properly" with getopts for simple processing.
Try this.
#!/bin/bash
# This script monitors the CPU temperature (in degrees C) over time, and
# records the data to a CSV file.
# Written by Ben Cottrell
function start_log()
{
touch "$output_csv"
read -p 'Enter the time interval for collecting data in seconds: ' t
if hash acpi 2>/dev/null; then
echo -e "time,temperature\n" > "$output_csv"
while (true); do
sleep $t
echo -e "$(date +%T),$(acpi -t | awk '{print $4}')\n" >> "$output_csv"
done
else
echo "Error: acpi is not installed. Please execute \"sudo apt-get install acpi, before running this script.\""
fi
}
if [ -n "$1" ]; then
local output_csv = $1
start_log
else
echo -e "Error: No filename specified.\n"
fi
echo -e "\n"

Resources