We are writing a bash script which basically does the job of python dictionary. Below is the code siffet we are using and the expected output.   #!/bin/bash declare -A serviceTag serviceTag["source"]="ccr" declare -A services services+=( ["dataservice"]="latest" )  serviceTag+=( ["services"]=services )  echo "$serviceTag"   The expected output is   {"source":"ccr","services":{"datasetvice":"latest"}}   But what we are getting is   ccrservices   Can somebody help us in what mistake we are doing here and how can we achieve this using bash and its code?  Regards, Kanthu
122787
Is there a way to print an entire array ([key]=value) without looping over all elements?    Assume I have created an array with some elements:    declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef)   I can print back the entire array with  for i in "${!array[@]}" do echo "${i}=${array[$i]}" done   However, it seems bash already knows how to get all array elements in one "go" - both keys ${!array[@]} and values ${array[@]}.    Is there a way to make bash print this info without the loop?  Edit: typeset -p array does that! However I can't remove both prefix and suffix in a single substitution:  a="$(typeset -p array)" b="${a##*(}" c="${b%% )*}"   Is there a cleaner way to get/print only the key=value portion of the output?
62905
Look at this if block:  #!/bin/bash  str="m.m" if [[ "${str}" =~ "m\.m" ]]; then     echo "matched" else     echo "not matched"     exit 1 fi  exit 0   This should print "matched", but it doesn't. Where am I going wrong?
11588
I'm trying to use Bash string substitution to combine an array. Desired output: cat,dog,fish  This works, but isn't there a way to do with without piping? #!/bin/bash  words=(cat dog fish)  echo &quot;${words[@]}&quot; | sed 's/ /,/g'  I've been trying this, which doesn't work. echo &quot;${words[@]// /,/}&quot;
66316
Trying to get this bash script to work:  #!/bin/bash  for result in $(cat ./fn7); for dip1 in $(cat ./fn4); for dip2 in $(cat ./fn5); for dip3 in $(cat ./fn6); for ip in $(seq 0 255); ifconfig &gt; ./fn1; cat ./fn1 |grep "broadcast" &gt; ./fn2; cat ./fn2 |cut -d " " -f10 &gt; ./fn3; cat ./fn3 |cut -d "." -f1 &gt; ./fn4; cat ./fn3 |cut -d "." -f2 &gt; ./fn5; cat ./fn3 |cut -d "." -f3 &gt; ./fn6; echo $dip1.$dip2.$dip3. &gt; ./fn7; ping -c 1 $result.$ip &gt; ./results   It fails with a error of:  ./IP-Sweeper.sh: line 4: syntax error near unexpected token `for' ./IP-Sweeper.sh: line 4: `for dip1 in $(cat ./fn4);'
90858
I am trying to do the following in my script:  #/bin/bash -x var1=www var2=www2 var3=www3 var4=www4 for i in 1 2 3 4; do    echo \$var$i done   Obviously, this doesn't work as expected and results in the following output:  $var1 $var2 $var3 $var4   How can I dynamically reference the variables in my loop?
98122
I can't figure out how to declare a variable inside bash -c command.   bash -c "var=3; echo $var"
66342
When I create  alias wd='ps -ef | grep java | awk {'print $2 " " $9'} | egrep "(A|B|C|D)"'   or  function wd () {     ps -ef | grep java | awk '{print $2}' ... }   in my .bashrc file, I get errors.  Interestingly, if I source my .bashrc file with the function, it 'compiles', but when executing, gives me:   context is      &gt;&gt;&gt; \ &lt;&lt;&lt; {\print     missing }   Can someone help me with this, and also answer when its better to put something in a function versus in an alias?
112122
Can anybody explain to me what the shell does in the two examples A) and B) below? It obviously behaves differently, but I can't find out why the output is different.  Example: Let's have a script in our current directory named bla.sh with only one command: echo ${0##/*} hello  A) Started as: ./bla.sh gives: ./bla.sh hello  B) Started as: . bla.sh gives: -bash hello  Since I use this in a script, the second output (because of the "-" in front of the -bash) kills the command. Of course, a simple -- before the ${...} helped, but I would love to understand what causes the output in the first place.I love bash. And vi[m]. But I digress…
100570
I have here a bash script:  #!/bin/bash  TARGET_FILE=ping_result.txt  # declare the target ip addresses declare -a ips=("XXX.XXX.XXX.XXX" "YYY.YYY.YYY.YYY")  function print_and_log() {     echo "$1"     echo "$1" &gt;&gt; $TARGET_FILE 2&gt;&amp;1 }  # loop through all ip addresses for i in "${ips[@]}" do     print_and_log "----- Begin Pinging for $i --- "     print_and_log "command: ping -c10 $i"     print_and_log $(ping -c10 $i)     print_and_log "----- Pinging for $i end ----- " done   My aim is to print the same output into a file and into console. But when I disconnect my operation system from the network, then in the console, I see (for example for the first IP address):  ----- Begin Pinging for XXX.XXX.XXX.XXX ---  command: ping -c10 XXX.XXX.XXX.XXX connect: Network is not reachable  ----- Pinging for XXX.XXX.XXX.XXX end -----    But in the file, I do no see any message, that the Network is not reachable. Why? How can I change my bash script, that I also can see this message in my logging file.
97554
I need to include below python script inside a bash script.   If the bash script end success, I need to execute the below script:  #!/usr/bin/python     from smtplib import SMTP import datetime debuglevel = 0  smtp = SMTP() smtp.set_debuglevel(debuglevel) smtp.connect('192.168.75.1', 25) smtp.login('my_mail', 'mail_passwd')  from_addr = "My Name &lt;my_mail@192.168.75.1&gt;" to_addr = "&lt;my_mail@192.168.75.1" subj = "Process completed" date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" ) #print (date) message_text = "Hai..\n\nThe process completed."  msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )  smtp.sendmail(from_addr, to_addr, msg) smtp.quit()
117426
Why when I try to write a function just in one line into .bashrc file,  list(){ ls -a }   I get error?  bash: /home/username/.bashrc: line num: syntax error: unexpected end of file   but when I write it in multi line it's ok?  list(){     ls -a }
91886
I wrote a simple script   #!/bin/bash -x  selentry=`ls -l / | sort ; ts=${PIPESTATUS[0]}`  echo $ts   but $ts show nothing. How I can display $ts variable, or how I can get exit status code from command in variable?  selentry=`ls -l / | sort`
14201
I'm not giving the real example as the post becomes so long that no one would read it. So I'll give other examples I have a bash script name.sh and it is programmed as below- #!/bin/bash echo &quot;Please Enter your name&quot; read name echo &quot;Hello $name&quot; sudo bash age.sh  And another script(say age.sh) like this- #!/bin/bash *echo &quot;Hello $name&quot; echo &quot;Please enter your age&quot; read age echo &quot;Thank you&quot;  *Here in the second script I want the variable $name from the first script to be used with the same value in the second script so the user doesn't have to type his name again Of course if I'm declaring a variable then I can do it in the other script too but if the user is declaring it then I can't predict what the user will answer. So how do I transfer the same value over to the other script? I have another idea to do it It goes like this- #!/bin/bash echo &quot;Please Enter your name&quot; read name echo &quot;Hello $name&quot; sudo bash age.sh --variable_value  Somehow can i make the 2nd script use the values given after -- and assign the same variables to them?
96825
Say I have the following script:  #!/bin/bash for i in $(seq 1000) do     cp /etc/passwd tmp     cat tmp | head -1 | head -1 | head -1 &gt; tmp  #this is the key line     cat tmp done   On the key line, I read and write the same file tmp which sometimes fails.  (I read it is because of race conditions because the processes in the pipeline are executed in parallel, which I do not understand why - each head needs to take the data from the previous one, doesn't it? This is NOT my main question, but you can answer it as well.)  When I run the script, it outputs about 200 lines. Is there any way I can force this script to output always 0 lines (so the I/O redirection to tmp is always prepared first and so the data is always destroyed)? To be clear, I mean changing the system settings, not this script.  Thanks for your ideas.
121768
I'd like to do a one line bash command that automatically does this:  screen -S myserver  python myserver.py  # inside the new screen CTRL A, D (detach)   I think this won't work:  screen -S myserver &amp;&amp; python myserver.py   because python myserver.py won't be started inside the screen.
30577
#!/bin/bash echo "$(curl -s -I https://google.com|grep Server)" abc   returns:  abcer: gws   and bash -x shows:    ++ grep Server ++ curl -s -I https://google.com ' abco 'Server: gws  abcer: gws   but    echo abc "$(curl -s -I https://google.com|grep Server)"`    returns  abc Server: gws   In other words, if I add text AFTER the curl command, it gets mangled, but it's ok if I add it before curl. What is happening? I cannot figure it out.
69705
Below is a complete copy of a demo I'm using to to figure out to get the sed command to get rid of the spaces in a persons name, and compress it down to not have spaces.  Once this is done, I want to assign it to the variable comp then I can re-use it later on in the script. Here I am just trying to echo it to the stdout so I can see it worked.  If I run the script and enter my name as Ronald McDonald the result I get returned is RonaldMcDonald} with that curly brace on the end of his name, or whatever I type in.  How do I get it to work, so that the result doesn't append the } to the back of the assigned text.  #!/bin/bash  function readName {     echo "Enter your full name:"     read fullName     clear }    # end readName  function cmprsName {     comp={ echo "$fullName" } | sed 's/ //g' }    # end cmprsName  function sayItNow {     echo $comp }    # end sayItNow  function allTogether {     readName     cmprsName     sayItNow }    #end allTogether  case $1 in         -h | --help )           allTogether                                 exit                                 ;;         * )                     echo "$0 -h"                                 exit 1 esac