In emacs , when I place the cursor on a } the echo buffer displays the corresponding content of the matching {
for example:
if(a==b){
.
.
.
}
placing my cursor on } would display " if (a==b). "
Googling helped find this plugin that sounded similar
https://github.com/vim-scripts/tEchoPair/blob/master/README
I'm new to vim. I installed the plugin but I don't get the intended result.
Is there a better way to get the matching parenthesis text? If not, how do I use this plugin ?
I tried to code a tiny script ShowMatchBrace.vim (that you can put it in your .vimrc) which displays the matched line for "}" and ")" during normal mode. (You are free to do whatever you want with it)
autocmd! CursorMoved * call <SID>MatchBraces()
"This variable is for redrawing the cmd-line"
if !exists("s:brace")
let s:brace=0
endif
function! s:MatchBraces()
let l:currentPos=getpos('.')
if getline('.')[col('.') - 1] =~# '\v\}|\)'
let s:brace=1
normal! %
if getline('.')[col('.')-1] =~# '\v\{|\('
echo getline('.')
else
echohl ErrorMsg | echomsg "No match found for }|)" | echohl None
endif
elseif s:brace ==# 1
call feedkeys("\<c-l>")
let s:brace=0
endif
call setpos('.',l:currentPos)
endfunction
Demo1
1 The Demo was before adding little changes to the code to include ")" case but it behaves exactly the same way as "}".
Related
I am trying to add folder paths to an array. I looked up on the internet and saw this solution. I gave it a try but I get an error message.
My code:
LOCALSITES=()
for d in "$DIRLOC"/*; do
${LOCALSITES}+='foo' #doesnt work
done
echo "${LOCALSITES[*]}"
Error message:
showSites.sh: line 35: +=: command not found
${LOCALSITES}+='foo'
will interpret the current value of that variable, giving you an empty string and therefore making the command read as:
+='foo'
You can see this if you do set -x beforehand:
pax:~$ set -x ; ${xx}+='foo' ; set +x
+ set -x
+ +=foo
+ '[' -x /usr/lib/command-not-found ']'
+ /usr/lib/command-not-found -- +=foo
+=foo: command not found
+ return 127
+ set +x
It's no real different to:
pax:~$ xx=1
pax:~$ ${xx}=2 # should be xx=2
1=2: command not found
To properly append to the array, you need to do:
LOCALSITES+=('foo')
Other things you may want to consider, courtesy of one Gordon Davisson in a comment:
It's probably best to use lower- or mixed-case variable names to avoid conflicts with the many all-caps variables with special functions: localsites+=('foo').
Make sure you select the correct quotes for whether you wish to interpret variables or not: localsites+=('foo') ; localsites+=("${newSite}").
You almost never want [*]. Use [#] instead, and put double-quotes around it: echo "${localsites[#]}".
I have one ini configuration file, I need to create a shell script using this configuration. What is the easiest method to access all variable, Can be used effectively from the shell script.
Can I use an array or something? Now planing to find the count of [] brackets then through awk get all variables one by one. Please suggest if any easiest way to effectively
cat app.ini
Below the output of my sample configuration file. Can be N no of Blocks.
[APP1]
name=Application1
StatusScript=/home/status_APP1.sh
startScript=/home/start_APP1.sh
stopScript=/home/stop_APP1.sh
restartScript=/home/restart.APP1.sh
logdir=/log/APP1/
[APP2]
name=Application2
StatusScript=/home/status_APP2.sh
startScript=/home/start_APP2.sh
stopScript=/home/stop_APP2.sh
restartScript=/home/restart.APP2.sh
logdir=/log/APP2/
.
.
.
.
.
[APPN]
name=ApplicationN
StatusScript=/home/status_APPN.sh
startScript=/home/start_APPN.sh
stopScript=/home/stop_APPN.sh
restartScript=/home/restart.APPN.sh
logdir=/log/APPN
/
Consider using a library like bash-ini-parser https://github.com/albfan/bash-ini-parser. It covers a lot of nuances like indentation, whitespaces, comments etc.
The example for your case may look like this:
#!/bin/bash
. bash-ini-parser
cfg_parser app.ini
cfg_section_APP1
echo $name
cfg_section_APP2
echo $logdir
cfg_section_APPN
echo $logdir
Below line help us to locate particular values in each section.
sed -nr "/^\[APP1\]/ { :l /^name[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" app.ini
trying to look up files in directories with wildcard *, and put the names into an array
the files have similar names (MATCHr1, MATCHr2 ... )
the problem arises when the file does not exist (which is a possibility)
set command returns a "no match" error and terminates the loop
if this happens how can i get it handle the error by jumping to the next iteration?
set SUBIDS = (10003 10005 10006)
foreach SUBID ($SUBIDS)
foreach SEQR ( MATCH ENC NBACK SIMON FACE )
ls -l *${SEQR}*.nii.gz
set Array = *${SEQR}*.nii.gz
echo $Array[*]
....rest of code works to use contents of Array to create text files
and works great when the *${SEQR}*.nii.gz returns a file name
but fails when the no matches are made with the wildcard
Any help would be well apreciated
thanks!
you can do you ls and check the $status (or $STATUS, depends on your system) flag:
ls -l *${SEQR}*.nii.gz >> /dev/null ; if !$status then ...
I am getting the following error running cmake
/build.make:3687: *** missing separator. Stop.
Line 3687 that has the error:
game_rUnversioned directory_32_OBJECTS = \
What is wrong there?
The immediate answer is, you cannot create variable names that contain whitespace. That's not valid in (newer versions of) make. So this:
game_rUnversioned directory_32_OBJECTS = \
is not a valid variable assignment because of the space (it has nothing to do with the backslash).
The longer answer is that your script ${CMAKE_CURRENT_SOURCE_DIR}/svn_version.sh which is apparently supposed to print the SVN version, is instead printing the string Unversioned directory. You'll have to figure out why that is and get that script to print the right value, or at least ensure that whatever value it prints is a single word and does NOT contain whitespace, before this will work.
ETA:
If you want to make this work in a directory which is not an SVN workspace, you'll need to fix the svn_version.sh script so it can handle the case where it can't find a version. Rewrite that script something like this:
#!/bin/sh
ver=$(svnversion -n -c game | cut -d':' -f2)
case $ver in
(*\ *) echo unknown ;;
(*) echo "$ver" ;;
esac
exit 0
This ensures that if the game directory isn't an SVN directory, it will print a value that doesn't contain any spaces (unknown) and this means your makefiles won't break.
That bare backslash looks very suspicious. That will be interpreted by Make as an attempt to continue that line on the next (physical) line, so what does that line contain?
I'm quite new to bash scripting and i've been working on a small bash file that can do a few things for me. First of all, i'm calling this from a C function using system() and i'd like the script to return a value (1 for error, 0 for OK). Is this possible?
int kinit() {
int err = system("/home/bluemoon/Desktop/GSSExample/kinitScript.sh");
}
Second, using Zenity, i managed to create a pop up window to insert user/password. Now, according to what the user does, multiple things should happen. If he closes the window or clicks "cancel", nothing should happen. If he clicks "OK", then i should check the input (for empty text boxes or something).
Assuming a correct input, i will use Expect to run a kinit program (it's a promt related with Kerberos) and log in. Now, if the password is correct, the prompt closes and the script should return 0. If it's not, the prompt will show something like "Kinit: user not found". I wanted to, in any case of error (closing window, clicking cancel or wrong credentials) return 1 in my script and return 0 on success.
#!/bin/bash
noText=""
ENTRY=`zenity --password --username`
case $? in
0)
USER=`echo $ENTRY | cut -d'|' -f1`
PW=`echo $ENTRY | cut -d'|' -f2`
if [ "$USER"!="$noText" -a "$PW"!="$noText" ]
then
/usr/bin/expect -c 'spawn kinit '`echo $USER`'; expect "Password for '`echo $USER`':" { send "'`echo $PW`'\r" }; interact'
fi
;;
1)
echo "Stop login.";;
-1)
echo "An unexpected error has occurred.";;
esac
My if isn't working properly, the expect command is always run. Cancelling or closing the Zenity window also always lead to case "0". I've also tried to return a variable but it says i can only return vars from inside functions?
Well, if anyone could give me some pointers, i'd appreciate it.
Dave
i'd like the script to return a value
Sure, just use exit in appropriate places
exit: exit [n]
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
My if isn't working properly, the expect command is always run.
if [ "$USER" != "$noText" -a "$PW" != "$noText" ]
# ^ ^ ^ ^
# | | | |
# \----- notice spaces ----/