I was doing my first bashrc configuration, i searched online for examples and i tried to do this but it gives me those errors:
I removed the colors code for a better reading.
bash: /home/filippo/.bashrc: line 228: syntax error near unexpected token `)'
bash: /home/filippo/.bashrc: line 228: ` fi')$BYellow$PathShortcut$Color_Off'\$ "; \'
HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
shopt -s checkwinsize
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u#\h: \w\a\]$PS1"
;;
*)
;;
esac
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
if [ -f ~/.git-prompt.sh ]; then
. ~/.git-prompt.sh
fi
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
Color_Off="\[\033[0m\]" # Text Reset
Time12h="\t"
Time12a="\#"
PathShort="\W"
PathFull="\w"
NewLine="\n"1
Jobs="\j"
export PS1=$BGreen'\u#\h'$Color_Off'$(git branch &>/dev/null;\
if [ $? -eq 0 ]; then \
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
if [ $? -eq 0 ]; then \
# #4 - Clean repository - nothing to commit
echo "'$Green'"$(__git_ps1 " (%s)"); \
else \
echo "$(echo `git status` | grep "to be committed" > /dev/null 2>&1; \
if [ $? -eq 0 ]; then
echo "'$IPurple'"$(__git_ps1 " {%s}"); \
else \
# #5 - Changes to working tree
echo "'$IRed'"$(__git_ps1 " {%s}"); \
fi)
fi) '$BYellow$PathShort$Color_Off'\$ "; \
else \
# #2 - Prompt when not in GIT repo
echo " '$BIBlue$PathShort$Color_Off'\$ "; \
fi)'
*) return;;
return: can only `return' from a function or sourced script
To locate more errors run your script with:
bash -x script.sh
Related
I am using ubuntu 18.04 to run a shell script, and the eval expression returns an exit code 1, even if I remove the git clone and the echo right before:
#!/bin/bash
set -e
export DEBIAN_FRONTEND=noninteractive
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.10.2
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc
eval "$(cat ~/.bashrc | tail -n +10)"
...
And here's the tail -n +10 ~/.bashrc (I just want to ignore the first 10 lines, specially where it says # If not running interactively, don't do anything):
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user#host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u#\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
If I run the same eval expression within the command line, and do echo $? it returns 0.
Doing exec bash, source ~/.bashrc or . ~/.bashrc as a substitute of the eval does not work as I am using a non-interactive shell.
Also, I can't remove set -e as I am using packer to build an image and I want to stop the build If I get an error.
Any idea what it could be?
Maven doesn't have a great way to dynamically prompt for a UID / Password during a;
mvn deploy
command;
#Adligo
This can be accomplished using the following deploy.sh bash script;
#!/bin/bash
#Expand new lines \n
shopt -s xpg_echo
# Thanks https://medium.com/#Drew_Stokes/bash-argument-parsing-54f3b81a6a8f
while (( "$#" )); do
case "$1" in
-a | --artifactory_server) artServer="$2" ; shift 2 ;;
-b | --bearer_token) oauthToken="$2" ; shift 2 ;;
-d | --debug) debug=1 ; shift 1 ;;
-e | --oauth_server) oauthServer="$2" ; shift 2 ;;
-g | --group_path) groupPath="$2" ; shift 2 ;;
-l | --load_properties) loadProperties="y" ; shift 1 ;;
-m | --retry_seconds) retrySecs="$2" ; shift 2 ;;
-n | --gen-token-opts) genTokenOpts="$2" ; shift 2 ;;
-o | --oauth_uid) oauthUid="$2" ; shift 2 ;;
-p | --password) password="$2" ; shift 2 ;;
-r | --retrys) retrys="$2" ; shift 2 ;;
-s | --oauth_password) oauthPass="$2" ; shift 2 ;;
-t | --auth_type) authType="$2" ; shift 2 ;;
-u | --uid) uid="$2" ; shift 2 ;;
-v | --version) version="$2" ; shift 2 ;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS="$PARAMS $1"
shift
;;
esac
done
if [[ "y" == "$loadProperties" ]]; then
echo "loading defaults from deployProperties.sh "
. ./deployProperties.sh
fi
# Overwrite the cli options using the values in ./deployProperties.sh when not present
# Alphabetized
if [[ -z $artServer ]]; then
artServer=$ART_SERVER
fi
if [[ -z $authType ]]; then
authType=$AUTH_TYPE
fi
if [[ -z $debug ]]; then
debug=$DEBUG
fi
if [[ -z $oauthPass ]]; then
oauthPass=$OAUTH_PASS
fi
if [[ -z $oauthServer ]]; then
oauthServer=$OAUTH_SERVER
fi
if [[ -z $oauthToken ]]; then
oauthToken=$OAUTH_TOKEN
elif [[ "none" == $oauthToken ]]; then
oauthToken=""
fi
if [[ -z $oauthUid ]]; then
oauthUid=$OAUTH_UID
fi
if [[ -z $genTokenOpts ]]; then
genTokenOpts=$GENERATE_TOKEN_OPTS
fi
if [[ -z $groupPath ]]; then
groupPath=$GROUP_PATH
fi
if [[ -z $password ]]; then
password=$PASSWORD
fi
if [[ -z $retrys ]]; then
retrys=$RETRYS
fi
if [[ -z retrySecs ]]; then
retrySecs=$RETRY_SECS
fi
if [[ -z $version ]]; then
version=$VERSION
fi
if [[ -z $uid ]]; then
uid=$UID
fi
#Begin Logic
if [[ "$debug" = "1" ]]; then
echo "executing with parameters\n"
echo "artServer=$artServer"
echo "authType=$authType"
echo "debug=$debug"
echo "oauthPass=$oauthPass"
echo "oauthToken=$OoauthToken"
echo "oauthUid=$oauthUid"
echo "groupPath=$groupPath"
echo "password=$password"
echo "retrys=$retrys"
echo "retrySecs=$retrySecs"
echo "password=$password"
echo "uid=$uid"
echo "version=$version"
fi
if [[ -z $version ]]; then
echo No Version, exiting
exit 1
fi
function exec {
if [[ "$debug" = "1" ]]; then
echo "executing\n$1"
fi
status=$?
eval $1
status=$?
times=$(($retrys))
if [[ $times -lt 1 ]]; then
times=$((1))
fi
sleepTime=$(($retrySecs))
if [[ sleepTime -lt 1 ]]; then
sleepTime=$((10))
fi
count=$((1))
while [ $count -lt $times ]
do
echo "count is $count"
if [[ $status == 0 ]]; then
echo "command sucessful"
return 0
else
echo "Waiting $sleepTime seconds ..."
sleep $sleepTime
eval $1
status=$?
count=$(($count+1))
fi
done
}
jar=`ls target | grep jar`
echo Deploying the following to ART_SERVER is
echo $artServer
echo $jar
if [[ -f "pom.xml" ]]; then
echo "Found pom.xml file!"
else
echo "There is NO pom.xml file exiting;"
exit 0;
fi
if [[ "simple" = "$authType" ]]; then
if [[ -z $password ]]; then
read -s -p "Password: " password
fi
retrys=$(($retrys))
if [[ $retrys -le 1 ]]; then
echo "retrys are -le 1 running\ncurl -u $artUid:$password -X PUT \"$artServer/$groupPath/$version/$jar\" -T target/$jar"
curl -u $artUid:$password -X PUT "$artServer/$groupPath/$version/$jar" -T target/$jar
echo "retrys are -le 1 running\ncurl -u $artUid:$password -X PUT \"$artServer/$groupPath/$version/pom.xml\" -T pom.xml"
curl -u $artUid:$password -X PUT "$artServer/$groupPath/$version/pom.xml" -T pom.xml
else
exec "curl -u $artUid:$password -X PUT \"$artServer/$groupPath/$version/$jar\" -T target/$jar"
exec "curl -u $artUid:$password -X PUT \"$artServer/$groupPath/$version/pom.xml\" -T pom.xml"
fi
# note md5 checksums are hidden by default
# https://jfrog.com/knowledge-base/how-to-show-the-checksum-files-when-browsing-artifacts-from-the-direct-url/
elif [[ "oauth" = "$authType" ]]; then
if [[ -z $oauthUid ]]; then
read -p "OAuth User Id: " oauthUid
fi
if [[ -z $oauthToken ]]; then
if [[ -z $oauthPass ]]; then
read -s -p "OAuth Password: " oauthPass
fi
status=$?
cmd="curl -XPOST $oauthServer $generateTokenOpts --data-urlencode \"username=$oauthUid\" --data-urlencode \"password=$oauthPass\""
status=$?
if [[ "0" == "$status" ]]; then
echo "Unable to contatct the following server, is it down?"
echo "$oauthServer"
exit 0;
fi
oauthResponce=`eval $cmd`
if [[ "$DEBUG" = "1" ]]; then
echo "\n\oauthResponce\n$oauthResponce"
echo "\n\n\n parsing token"
fi
oauthToken=`echo $oauthResponce | sed "s/{.*\"access_token\":\"\([^\"]*\).*}/\1/g"`
echo "\n\n\nGot the following token;"
echo $oauthToken
else
echo "Using provided oauthToken"
fi
retrys=$(($retrys))
if [[ $retrys -le 1 ]]; then
echo "retrys are -le 1 running\ncurl -u $oauthUid:$oauthToken -X PUT \"$artServer/$groupPath/$version/$jar\" -T target/$jar"
curl -u $oauthUid:$oauthToken -X PUT "$artServer/$groupPath/$version/$jar" -T target/$jar
echo "retrys are -le 1 running\ncurl -u $oauthUid:$oauthToken -X PUT \"$artServer/$groupPath/$version/pom.xml\" -T pom.xml"
curl -u $oauthUid:$oauthToken -X PUT "$artServer/$groupPath/$version/pom.xml" -T pom.xml
else
exec "curl -u $oauthUid:$oauthToken -X PUT \"$artServer/$groupPath/$version/$jar\" -T target/$jar"
exec "curl -u $oauthUid:$oauthToken -X PUT \"$artServer/$groupPath/$version/pom.xml\" -T pom.xml"
fi
else
echo "Unknown AUTH_TYPE must be one of (simple, oauth)"
fi
And a deployProperties.sh file like;
# ART short for Artifactory
ART_SERVER=http://localhost:8081/artifactory/libs-snapshot-local
#
# Try to keep this out of this file and use it from the
# CLI dialog which is a bit more secure :)
#
ART_PASSWORD=
#
# This is the uid of the ART_SERVER
#
ART_UID=admin
#
# This is either simple or oauth (for OAuth bearer tokens)
#
AUTH_TYPE=simple
# 0 is off 1 is on
DEBUG=0
GENERATE_TOKEN_OPTS=`echo -d "instance=maven"`
GROUP_PATH=
RETRYS=3
RETRY_SECS=10
#
# It is NOT recommend to use this setting, as it is less secure
# than the OAUTH_TOKEN setting
#
OAUTH_PASS=
#
# This token MAY be created by this script and copied into this file
# NOTE there is NO option for a OAUTH_PASSWORD which is an intentional security feature
# use the CLI to dialog for the OAuth password
#
OAUTH_TOKEN=
OAUTH_SERVER=
#
# The User Identifier of the OAuth requests
#
OAUTH_UID=
VERSION=
export ART_SERVER ART_PASSWORD ART_UID AUTH_TYPE GENERATE_TOKEN_OPTS RETRYS RETRY_SECS OAUTH_PASS OAUTH_TOKEN OAUTH_SERVER OAUTH_UID TOKEN VERSION
This is now hosted here;
https://github.com/adligo/artifactory_deploy.sh.adligo.org
Please contribute and allow shorter answers to be posted to stack overflow :)
I am trying to source an auto-completion script as a non-root user but receive a 'Bad Substitution' error message.
I am able to source it as root though.
On a other server I am able to source the script as non-root user.
I am guessing it is not a permission issue since I receive the same error trying to source a copy of the script with full permissions.
I have tried to echo all the environment variables used in the script, no issue there.
As the auto-completion script is packaged with the software I use I would rather not modify it.
Anyone would have a hint on what could be missing to the user so I can source the script from it ?
Thanks in advance for any idea!
Edit1:
ps output:
PID TTY TIME CMD
3261 pts/0 00:00:00 bash
73620 pts/0 00:00:00 ps
Error only as non-root user:
/opt/splunk/share/splunk/cli-command-completion.sh: 7: /opt/splunk/share/splunk/cli-command-completion.sh: Bad substitution
Script:
# Vainstein K 12aug2013
# # # Check a few prereqs.
feature='"splunk <verb> <object>" tab-completion'
[ `basename $SHELL` != 'bash' ] && echo "Sorry, $feature is only for bash" >&2 && return 11
[ ${BASH_VERSINFO[0]} -lt 4 ] && echo "Sorry, $feature only works with bash 4.0 or higher" >&2 && return 12
[ `type -t complete` != 'builtin' ] && echo "Sorry, $feature requires a bash that supports programmable command completion" >&2 && return 13
die () {
echo "(exit=$?) $#" >&2 && exit 42
}
ifSourced () { # do NOT exit(1) from this function!
local readonly tempfile=`pwd`/tmp--cli-completion--$$
rm -f $tempfile
$BASH ${BASH_ARGV[0]} --populateTempfile $tempfile
[ $? -eq 0 ] || return
[ -e $tempfile ] || return
. $tempfile
rm -f $tempfile
# # # Associate the completion function with the splunk binary.
local readonly completionFunction=fSplunkComplete
complete -r splunk 2>/dev/null
complete -F $completionFunction splunk
# You can view the completion function anytime via: $ type fSplunkComplete
}
ifInvoked () { # all error checking happens in this function
local readonly debug=false
local readonly tempfile=$1
$debug && echo "Told that tempfile=$tempfile"
# # # If anything goes wrong, at least we don't pollute cwd with our tempfile.
$debug || trap "rm -f $tempfile" SIGINT SIGQUIT SIGTERM SIGABRT SIGPIPE
touch $tempfile || die "Cannot touch tempfile=$tempfile"
# # # Decide where SPLUNK_HOME is.
if [ "$(dirname $(pwd))" == 'bin' ]; then
local readonly splunkHome=$(dirname $(dirname $(pwd)))
elif [ -n "$SPLUNK_HOME" ]; then
local readonly splunkHome=$SPLUNK_HOME
else
die 'Cannot figure out where SPLUNK_HOME is'
fi
$debug && echo "Decided SPLUNK_HOME=$splunkHome"
# # # Check that splunk (the binary) exists.
local readonly splunkBinary=$splunkHome/bin/splunk
[ -e $splunkBinary -a -x $splunkBinary ] || die "Cannot find expected binary=$splunkBinary"
# # # Find the file with object->{verb1,verb2,...} map.
local readonly splunkrcCmdsXml=$splunkHome/etc/system/static/splunkrc_cmds.xml
[ -e $splunkrcCmdsXml ] || die "Cannot find expected file $splunkrcCmdsXml"
$debug && echo "Shall read verb-obj info from: $splunkrcCmdsXml"
# # # Parse the map file, and generate our internal verb->{objA,objB,...} map.
local -A verb_to_objects
local line object verb objectsForThisVerb lineNumber=0
local inItem=false
local readonly regex_depr='\<depr\>'
local readonly regex_verb='\<verb\>'
local readonly regex_synonym='\<synonym\>'
while read line; do
lineNumber=$((lineNumber+1))
if $inItem; then
if [[ $line =~ '</item>' ]]; then
$debug && echo "Exited item tag at line=$lineNumber; this was obj=$object"
inItem=false
object=''
elif [[ $line =~ '<cmd name' && ! $line =~ $regex_depr && ! $line =~ $regex_synonym ]]; then
[ -z "$object" ] && die "BUG: No object within item tag. (At line $lineNumber of $splunkrcCmdsXml)"
verb=${line#*\"} # remove shortest match of .*" from the front
verb=${verb%%\"*} # remove longest match of ".* from the back
[ "$verb" == '_internal' ] && continue # Why the... eh, moving on.
objectsForThisVerb=${verb_to_objects[$verb]}
objectsForThisVerb="$objectsForThisVerb $object"
verb_to_objects[$verb]=$objectsForThisVerb
$debug && echo "Mapped object=$object to verb=$verb at line=$lineNumber; now objectsForThisVerb='$objectsForThisVerb'"
fi
else # ! inItem
if [[ $line =~ '<item obj' && ! $line =~ $regex_depr && ! $line =~ $regex_verb && ! $line =~ $regex_synonym ]]; then
inItem=true
object=${line#*\"} # remove shortest match of .*" from the front
object=${object%%\"*} # remove longest match of ".* from the back
$debug && echo "Entered item tag at line=$lineNumber, parsed object=$object"
[ "$object" == 'on' ] && inItem=false # Do not expose Amrit's puerile jest.
[ "$object" == 'help' ] && inItem=false # Although 'help' is a verb, splunkrc_cmds.xml constructs it as an object; ugh. We'll deal with the objects (topics) of 'splunk help' separately, below.
fi
fi
done < $splunkrcCmdsXml
$debug && echo "Processed $lineNumber lines. Map keys: ${!verb_to_objects[*]}, values: ${verb_to_objects[#]}"
# # # Oh wait, '<verb> deploy-server' aren't in splunkrc_cmds.xml; thanks, Jojy!!!!!
for verb in reload enable disable display; do
objectsForThisVerb=${verb_to_objects[$verb]}
objectsForThisVerb="$objectsForThisVerb deploy-server"
verb_to_objects[$verb]=$objectsForThisVerb
done
# # # Find the file with topics understood by 'splunk help <topic>' command, and extract list of topics.
local readonly literalsPy=$splunkHome/lib/python2.7/site-packages/splunk/clilib/literals.py
[ -e $literalsPy ] || die "Cannot find expected file $literalsPy"
$debug && echo "Shall read help topics list from: $literalsPy"
local readonly helpTopics=$(sed '/^addHelp/! d; s/^addHelp//; s/,.*$//; s/[^a-zA-Z_-]/ /g; s/^[ ]*//; s/[ ].*$//; /^$/ d' $literalsPy | sort | uniq)
$debug && echo "Parsed help topics list as: $helpTopics"
#######################################################
# # # Write the completion function to tempfile: BEGIN.
local readonly completionFunction=fSplunkComplete
echo -e 'function '$completionFunction' () {' >> $tempfile
echo -e '\tlocal wordCur=${COMP_WORDS[COMP_CWORD]}' >> $tempfile
echo -e '\tlocal wordPrev=${COMP_WORDS[COMP_CWORD-1]}' >> $tempfile
echo -e '\tcase $wordPrev in' >> $tempfile
# # # What can follow 'splunk' itself? Verbs used in main.c to key the 'cmd_handlers' array; and verbs from splunkrc_cmds.xml; and 'help'.
local readonly keys__cmd_handlers='ftr start startnoss stop restart restartss status rebuild train fsck clean-dispatch clean-srtemp validate verifyconfig anonymize find clean createssl juststopit migrate --version -version version httpport soapport spool ftw envvars _RAW_envvars _port_check cmd _rest_xml_dump search dispatch rtsearch livetail _normalizepath _internal logout btool pooling _web_bootstart offline clone-prep-clear-config diag'
local allVerbs="${!verb_to_objects[*]}"
echo -e '\t\tsplunk)\n\t\t\tCOMPREPLY=( $(compgen -W "'$keys__cmd_handlers $allVerbs' help" -- $wordCur) ) ;;' >> $tempfile
# # # What can follow 'splunk _internal'? see cmd_internal() of main.c
local readonly actions_internal='http mgmt https pre-flight-checks check-db call rpc rpc-auth soap-call soap-call-auth prefixcount totalcount check-xml-files first-time-run make-splunkweb-certs-and-var-run-merged'
echo -e '\t\t_internal)\n\t\t\tCOMPREPLY=( $(compgen -W "'$actions_internal'" -- $wordCur) ) ;;' >> $tempfile
# # # Options to 'splunk clean' are in CLI::clean() of src/main/Clean.cpp; to 'splunk fsck', in usageBanner of src/main/Fsck.cpp; to 'splunk migrate', in CLI::migrate() of src/main/Migration.cpp
echo -e '\t\tclean)\n\t\t\tCOMPREPLY=( $(compgen -W "all eventdata globaldata userdata inputdata locks deployment-artifacts raft" -- $wordCur) ) ;;' >> $tempfile
echo -e '\t\tfsck)\n\t\t\tCOMPREPLY=( $(compgen -W "scan repair clear-bloomfilter make-searchable" -- $wordCur) ) ;;' >> $tempfile
echo -e '\t\tmigrate)\n\t\t\tCOMPREPLY=( $(compgen -W "input-records to-modular-inputs rename-cluster-app" -- $wordCur) ) ;;' >> $tempfile
# # # List the help topics.
echo -e '\t\thelp)\n\t\t\tCOMPREPLY=( $(compgen -W "'$helpTopics'" -- $wordCur) ) ;;' >> $tempfile
# # # What can follow 'splunk cmd'? any executable in SPLUNK_HOME/bin/
echo -e '\t\tcmd)\n\t\t\tCOMPREPLY=( $(compgen -o default -o filenames -G "'$splunkHome'/bin/*" -- $wordCur) ) ;;' >> $tempfile
# # # Finally, let each verb be completed by its objects.
for verb in $allVerbs; do
echo -e '\t\t'$verb')\n\t\t\tCOMPREPLY=( $(compgen -W "'${verb_to_objects[$verb]}'" -- $wordCur) ) ;;' >> $tempfile
done
# # # And if we've run out of suggestions, revert to bash's default completion behavior: filename completion.
echo -e '\t\t*)\n\t\t\tCOMPREPLY=( $(compgen -f -- $wordCur) ) ;;' >> $tempfile
echo -e '\tesac' >> $tempfile
echo -e '}' >> $tempfile
$debug && cp $tempfile $tempfile~bak
# # # Write the completion function to tempfile: DONE.
######################################################
# # # Sanity check: source the tempfile, make sure that the function we wrote can be parsed and loaded by the shell.
unset $completionFunction
. $tempfile
[ "`type -t $completionFunction`" == 'function' ] || die 'BUG: generated completion function cannot be parsed by bash'
}
if [ $SHLVL -eq 1 ]; then
[ $# -ge 1 ] && echo "Ignoring supplied arguments: $#" >&2
ifSourced
elif [ $SHLVL -eq 2 ]; then
if [ $# -eq 2 ] && [ $1 == '--populateTempfile' ]; then
ifInvoked $2
else
echo -e "This script must be sourced, like so:\n\n\t\033[1m. $0\033[0m\n"
fi
else
: # user is running screen(1) or something of the sort.
fi
# # # Clean up.
unset die ifSourced ifInvoked
Edit2:
xtrace output
++ feature='"splunk <verb> <object>" tab-completion'
+++ basename /bin/bash
++ '[' bash '!=' bash ']'
++ '[' 4 -lt 4 ']'
+++ type -t complete
++ '[' builtin '!=' builtin ']'
++ '[' 1 -eq 1 ']'
++ '[' 0 -ge 1 ']'
++ ifSourced
+++ pwd
++ local readonly tempfile=/home/splunk/tmp--cli-completion--12431
++ rm -f /home/splunk/tmp--cli-completion--12431
++ /bin/sh cli-command-completion.sh --populateTempfile /home/splunk/tmp--cli-completion--12431
+ feature="splunk <verb> <object>" tab-completion
+ basename /bin/bash
+ [ bash != bash ]
cli-command-completion.sh: 8: cli-command-completion.sh: Bad substitution
++ '[' 2 -eq 0 ']'
++ return
++ unset die ifSourced ifInvoked
Edit3:
When using
set -o verbose
set -o noglob
set -o noglob
and checking differences between OK (root) and failing (non-root) run.
OK side:
[...]
+++ basename /bin/bash
++ '[' bash '!=' bash ']'
[ ${BASH_VERSINFO[0]} -lt 4 ] && echo "Sorry, $feature only works with bash 4.0 or higher" >&2 && return 12
++ '[' 4 -lt 4 ']'
[...]
++ local readonly tempfile=/home/splunk/tmp--cli-completion--42064
++ rm -f /home/splunk/tmp--cli-completion--42064
++ /bin/bash cli-command-completion.sh --populateTempfile /home/splunk/tmp--cli-completion--42064
+ set -o verbose
set -o noglob
+ set -o noglob
# Vainstein K 12aug2013
[...]
[ ${BASH_VERSINFO[0]} -lt 4 ] && echo "Sorry, $feature only works with bash 4.0 or higher" >&2 && return 12
+ '[' 4 -lt 4 ']'
[...]
Failing side:
[...]
+++ basename /bin/bash
++ '[' bash '!=' bash ']'
[ ${BASH_VERSINFO[0]} -lt 4 ] && echo "Sorry, $feature only works with bash 4.0 or higher" >&2 && return 12
++ '[' 4 -lt 4 ']'
[...]
++ local readonly tempfile=/home/splunk/tmp--cli-completion--40686
++ rm -f /home/splunk/tmp--cli-completion--40686
++ /bin/sh cli-command-completion.sh --populateTempfile /home/splunk/tmp--cli-completion--40686
+ set -o verbose
set -o noglob
+ set -o noglob
# Vainstein K 12aug2013
# # # Check a few prereqs.
feature='"splunk <verb> <object>" tab-completion'
+ feature="splunk <verb> <object>" tab-completion
[ `basename $SHELL` != 'bash' ] && echo "Sorry, $feature is only for bash" >&2 && return 11
+ basename /bin/bash
+ [ bash != bash ]
[ ${BASH_VERSINFO[0]} -lt 4 ] && echo "Sorry, $feature only works with bash 4.0 or higher" >&2 && return 12
cli-command-completion.sh: 10: cli-command-completion.sh: Bad substitution
++ '[' 2 -eq 0 ']'
++ return
# # # Clean up.
unset die ifSourced ifInvoked
++ unset die ifSourced ifInvoked
Seems like as non-root, it runs as /bin/sh whereas as 'bash' as root.
Weird because I tried multiple things to force bash there, maybe not the right ones.
It also fail at line 7 even though the first loop succeeds.
This should do the trick (line 7):
[ $(type -t complete) != 'builtin' ] && echo "Sorry, $feature requires a bash that supports programmable command completion" >&2 && return 13
Turns out from trace output that /bin/sh was used instead of /bin/bash to launch the script because /bin/bash was not set as default shell.
I have changed the default shell to /bin/bash and it is all ok now :)
I have used this method to set /bin/bash as default shell :
Check:
grep /etc/passwd
Change:
chsh --shell /bin/bash
Check modification:
grep /etc/passwd
Thanks for the help!
I didn't know how to solve this error.
bash: /home/dery/.bashrc: line 168: syntax error: unexpected end of file
Here is my .bashrc:
case $- in
*i*) ;;
*) return;;
esac
HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=1000
HISTFILESIZE=2000
shopt -s checkwinsize
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
fi
unset color_prompt force_color_prompt
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\$(print_title)\a\]$PS1"
__el_LAST_EXECUTED_COMMAND=""
print_title ()
{
__el_FIRSTPART=""
__el_SECONDPART=""
if [ "$PWD" == "$HOME" ]; then
__el_FIRSTPART=$(gettext --domain="pantheon-files" "Home")
else
if [ "$PWD" == "/" ]; then
__el_FIRSTPART="/"
else
__el_FIRSTPART="${PWD##*/}"
fi
fi
if [[ "$__el_LAST_EXECUTED_COMMAND" == "" ]]; then
echo "$__el_FIRSTPART"
return
fi
#trim the command to the first segment and strip sudo
if [[ "$__el_LAST_EXECUTED_COMMAND" == sudo* ]]; then
__el_SECONDPART="${__el_LAST_EXECUTED_COMMAND:5}"
__el_SECONDPART="${__el_SECONDPART%% *}"
else
__el_SECONDPART="${__el_LAST_EXECUTED_COMMAND%% *}"
fi
printf "%s: %s" "$__el_FIRSTPART" "$__el_SECONDPART"
}
put_title()
{
__el_LAST_EXECUTED_COMMAND="${BASH_COMMAND}"
printf "\033]0;%s\007" "$1"
}
# Show the currently running command in the terminal title:
# http://www.davidpashley.com/articles/xterm-titles-with-bash.html
update_tab_command()
{
# catch blacklisted commands and nested escapes
case "$BASH_COMMAND" in
*\033]0*|update_*|echo*|printf*|clear*|cd*)
__el_LAST_EXECUTED_COMMAND=""
;;
*)
put_title "${BASH_COMMAND}"
;;
esac
}
preexec_functions+=(update_tab_command)
;;
*)
;;
You forgot to close your case at the end of the script with esac. This leaves an unterminated case, causing Bash to reach the end of the file looking for one, triggering this error.
I've using a .bashrc from one of peepcode's screencast.
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$ '
# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user#host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PROMPT_COMMAND='echo -ne "\033]0;${USER}#${HOSTNAME}: ${PWD}\007"'
# ;;
#*)
# ;;
#esac
# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi
# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ]; then
case " $(groups) " in *\ admin\ *)
if [ -x /usr/bin/sudo ]; then
cat <<-EOF
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
EOF
fi
esac
fi
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found -- $1
return $?
else
return 127
fi
}
fi
. ~/bin/bash_colors.sh
#load bash aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Unbreak broken, non-colored terminal
export TERM='xterm-color'
alias ls='ls -G'
alias ll='ls -lG'
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
export GREP_OPTIONS="--color"
# Erase duplicates in history
export HISTCONTROL=erasedups
# Store 10k history entries
export HISTSIZE=10000
# Append to the history file when exiting instead of overwriting it
shopt -s histappend
function minutes_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1`
seconds_since_last_commit=$((now-last_commit))
minutes_since_last_commit=$((seconds_since_last_commit/60))
echo $minutes_since_last_commit
}
grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR=${RED}
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR=${YELLOW}
else
local COLOR=${GREEN}
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m${NORMAL}"
# The __git_ps1 function inserts the current git branch where %s is
local GIT_PROMPT=`__git_ps1 "(%s|${SINCE_LAST_COMMIT})"`
echo ${GIT_PROMPT}
fi
}
export PS1='$(grb_git_prompt)$ '
gd() { git diff $* | view -; }
gdc() { gd --cached $*; }
source ~/bin/git-completion.bash
I can confirm the error is in grb_git_promit. What happens is the second lines typed in shell is overwriting the first line. Can anyone could help me with this?
EDIT:
grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR="\[\033[0;31m\]"
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR="\[\033[0;37m\]"
else
local COLOR="\[\033[0;32m\]"
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m$"
# The __git_ps1 function inserts the current git branch where %s is
echo "$(__git_ps1 '(%s|')${SINCE_LAST_COMMIT}"
fi
}
When you have colors and other non-printing escape sequences in your prompt, you have to surround them with escaped single brackets. Here is a simple example:
PS1='\[\033[38;5;1m\]some red text\[\033[0m\]\$ '
Which will cause the prompt to display "some red text" in red and the dollar sign (or "#") in the default color.
Here is another way to do the same thing:
red='\033[38;5;1m'
none='\033[0m'
PS1='\[$red\]some red text\[$none\]\$ '
By the way, to do this more portably and have less complex build-up of variables:
red=$(tput setaf 1)
none=$(tput sgr0)
See man 5 terminfo for more information on the settings.