LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-16-2017, 07:36 PM   #1
watcher69b
Member
 
Registered: Nov 2007
Location: /home/watcher69b
Distribution: RH, Fedora & CentOS
Posts: 552

Rep: Reputation: 41
Help with bash script


Hello everyone. I have an issue/question I hope you can help me with...

I am trying write a bash script for checking code out of a online code repository. (I have no control over the repository)

If I run the bash script it will show the correct syntax (Shown via an echo statement) and if you then copy that syntax from the screen and run it manually at the command prompt it works.

However if you let it run through the script it fails as if the one of variable was not working.


The variable is currently set to
COMMAND=/bin/command
SERVER=server1
STATE='"User Testing"'
GETCODE='"*.*"'

syntax is:
$COMMAND -b $SERVER -sr $STATE -ex $GETCODE

/bin/command -b server1 -sr "User Testing" -ex "*.*"



Can anyone tell me why the script would echo the proper syntax but not run it? Or why I can run the same syntax manually but not via the script?

my hunch is that is is breaking the string "User Testing" up.

I have tired:
""User Testing""
'"User Testing"'
"\User\ Testing"
"User Testing"
 
Old 11-16-2017, 07:41 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,776

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
You need to quote the $STATE variable where it is used:
Code:
$COMMAND -b $SERVER -sr "$STATE" -ex $GETCODE
 
Old 11-16-2017, 07:42 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
After 500+ posts you should know to use code tags and paste the actual script and the output/error messages.
 
Old 11-16-2017, 07:52 PM   #4
watcher69b
Member
 
Registered: Nov 2007
Location: /home/watcher69b
Distribution: RH, Fedora & CentOS
Posts: 552

Original Poster
Rep: Reputation: 41
Quote:
Originally Posted by Keith Hedger View Post
After 500+ posts you should know to use code tags and paste the actual script and the output/error messages.
I know I was shocked too. Who knew

#!/bin/bash
# This file is designed to check code out of Harvest
#
# 11/15/2017
#
# All your Base Are Belongs to us
#
BASE_DIR=/build/testing #DO NOT ADD Trailing /
BUILD_DIR="$BASE_DIR/build"
BATCH_DIR="$BUILD_DIR/batch"
CHECKOUT_BASE_VIEW_PATH="SOME_Code_Project"
CHECKOUT_BROKER="Server1" #Name and IP must be in /etc/hosts. Cannot use FQDN
CHECKOUT_COMMAND=/opt/CA/scm/bin/hco
CHECKOUT_HARVEST_STATE=""USER TEST""
CHECKOUT_PROJECT="YEA_A_Project"
CHECKOUT_PN=""CHECK OUT FOR BROWSE/SYNCHRONIZE""
CHECKOUT_S=""*.*""
LOG_DIR="$BATCH_DIR/logs"
DEBUG=0

if [ $DEBUG == 1 ];then
echo -e "Debugging Information"
echo -e "BASE_DIR= $BASE_DIR"
echo -e "BUILD_DIR= $BUILD_DIR"
echo -e "BATCH_DIR= $BATCH_DIR"
echo -e "CHECKOUT_BASE_VIEW_PATH= $CHECKOUT_BASE_VIEW_PATH"
echo -e "CHECKOUT_BROKER= $CHECKOUT_BROKER"
echo -e "CHECKOUT_COMMAND= $CHECKOUT_COMMAND"
echo -e "CHECKOUT_HARVEST_STATE= $CHECKOUT_HARVEST_STATE"
echo -e "CHECKOUT_PROJECT= $CHECKOUT_PROJECT"
echo -e "CHECKOUT_PN= $CHECKOUT_PN"
echo -e "CHECKOUT_S= $CHECKOUT_S"
echo -e "LOG_DIR= $LOG_DIR"
echo -e "DEBUG= $DEBUG\n"
echo -e "I Would be running the following commands to check out code from $CHECKOUT_BROKER:\n"
echo -e "\n\n"
echo "$CHECKOUT_COMMAND -b $CHECKOUT_BROKER -usr browse -pw password -vp $CHECKOUT_BASE_VIEW_PATH/batch/Scripts -en $CHECKOUT_PROJECT -st $CHECKOUT_HARVEST_STATE -pn "$CHECKOUT_PN" -cp $BUILD_DIR/Scripts -br -r -ced -op pc -s "$CHECKOUT_S" -o $LOG_DIR/outputScripts.log"
echo -e "\n\n"
fi

if [ $DEBUG != 1 ];then
echo -e "Fetching the code from $CHECKOUT_BROKER"

echo -e "Cleaning checked out code directory $BUILD_DIR ...."
#if the $BUILD_DIR exists, wack it.
if [ -d $BUILD_DIR ];then
rm -rf $BUILD_DIR
fi

#if the $BUILD_DIR does NOT exist, create it.
if [ ! -d $BUILD_DIR ];then
echo -e "Creating directory $BUILD_DIR ...."
mkdir -p $BUILD_DIR
fi

#if the $BATCH_DIR does NOT exist, create it.
if [ ! -d $BATCH_DIR ];then
echo -e "Creating directory $BATCH_DIR ...."
mkdir -p $BATCH_DIR
fi

#if the $LOG_DIR does NOT exist, create it.
if [ ! -d $LOG_DIR ];then
echo -e "Creating directory $LOG_DIR ...."
mkdir -p $LOG_DIR
fi

echo -e "Checking out code from... $CHECKOUT_BASE_VIEW_PATH"
CHECKOUT_ERROR=0

#Harvest Checkout Commands
echo -e "Checking out $CHECKOUT_BASE_VIEW_PATH/batch/Config from $CHECKOUT_HARVEST_STATE"
$CHECKOUT_COMMAND -b $CHECKOUT_BROKER -usr user1 -pw password -vp $CHECKOUT_BASE_VIEW_PATH/batch/config -en $CHECKOUT_PROJECT -st $CHECKOUT_HARVEST_STATE -pn $CHECKOUT_PN -cp $BUILD_DIR/Config -br -r -ced -op pc -s "$CHECKOUT_S" -o $LOG_DIR/outputConfig.log

echo -e "Checking out $CHECKOUT_BASE_VIEW_PATH/batch/copybooks from $CHECKOUT_HARVEST_STATE"
$CHECKOUT_COMMAND -b $CHECKOUT_BROKER -usr user1 -pw password -vp $CHECKOUT_BASE_VIEW_PATH/batch/copybooks -en $CHECKOUT_PROJECT -st $CHECKOUT_HARVEST_STATE -pn $CHECKOUT_PN -cp $BUILD_DIR/Copybooks -br -r -ced -op pc -s $CHECKOUT_S -o $LOG_DIR/outputCopybooks.log

echo -e "Checking out $CHECKOUT_BASE_VIEW_PATH/batch/csf from $CHECKOUT_HARVEST_STATE"
$CHECKOUT_COMMAND -b $CHECKOUT_BROKER -usr user1 -pw password -vp $CHECKOUT_BASE_VIEW_PATH/batch/csf -en $CHECKOUT_PROJECT -st $CHECKOUT_HARVEST_STATE -pn $CHECKOUT_PN -cp $BUILD_DIR/CSF -br -r -ced -op pc -s $CHECKOUT_S -o $LOG_DIR/outputCSF.log


#Works from command line
# /opt/CA/scm/bin/hco -b Server1 -usr user1 -pw password -vp SOME_Code_Project/batch/scripts -en YEA_A_Project -st "USER TEST" -pn "CHECK OUT FOR BROWSE/SYNCHRONIZE" -cp /build/testing/build/Scripts -br -r -ced -op pc -s "*.*" -o /build/testing/build/log/outputScripts.log

echo -e "Code checkout complete..."
fi
 
Old 11-16-2017, 08:01 PM   #5
watcher69b
Member
 
Registered: Nov 2007
Location: /home/watcher69b
Distribution: RH, Fedora & CentOS
Posts: 552

Original Poster
Rep: Reputation: 41
There is no real output, just an error in the logs saying unknown state. I can reproduce the error manually by removing the quotes from
USER TEST.


/opt/CA/scm/bin/hco -b Server1 -usr user1 -pw password -vp SOME_Code_Project/batch/scripts -en YEA_A_Project -st "USER TEST" -pn "CHECK OUT FOR BROWSE/SYNCHRONIZE" -cp /build/testing/build/Scripts -br -r -ced -op pc -s "*.*" -o /build/testing/build/log/outputScripts.log
 
Old 11-17-2017, 12:15 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by watcher69b View Post
...
The variable is currently set to
COMMAND=/bin/command
SERVER=server1
STATE='"User Testing"'
GETCODE='"*.*"'

syntax is:
$COMMAND -b $SERVER -sr $STATE -ex $GETCODE

/bin/command -b server1 -sr "User Testing" -ex "*.*"
You normally must put quotes around variables in command arguments (unless you know there are no special characters, or you intend some special effects)
Code:
"$COMMAND" -b "$SERVER" -sr "$STATE" -ex "$GETCODE"
On the other hand you do not need quotes in a variable assignment unless there is a space
Code:
COMMAND=/bin/command
SERVER=server1
STATE="User Testing"
GETCODE=*.*
 
Old 11-19-2017, 10:42 AM   #7
watcher69b
Member
 
Registered: Nov 2007
Location: /home/watcher69b
Distribution: RH, Fedora & CentOS
Posts: 552

Original Poster
Rep: Reputation: 41
Thank you, I will give that a try
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration