Check via if condition that variable named &quot;var&quot; contains the contents of variable &quot;value&quot; and &quot;var&quot; must have more than 113,469 characters above described conditions are met, the script must then print last 20 characters of the variable &quot;var&quot;.  I am stuck at this question from a HTB Academy tutorial and tried out by different ways but still no luck with solution. Any help?  #!/bin/bash var=&quot;8dm7KsjU28B7v621Jls&quot; value=&quot;ERmFRMVZ0U2paTlJYTkxDZz09Cg&quot; for i in {1..40} do         var=$(echo $var | base64)                  #&lt;---- Need to frame an if condition here: done
13832
I have a string in Bash: string=&quot;My string&quot;  How can I test if it contains another string? if [ $string ?? 'foo' ]; then   echo &quot;It's there!&quot; fi  Where ?? is my unknown operator. Do I use echo and grep? if echo &quot;$string&quot; | grep 'foo'; then   echo &quot;It's there!&quot; fi  That looks a bit clumsy.
4389
I want to use $var in a shell brace expansion with a range, in bash. Simply putting {$var1..$var2} doesn't work, so I went "lateral"...   The following works, but it's a bit kludgey.    # remove the split files echo rm foo.{$ext0..$extN} rm-segments &gt; rm-segments source rm-segments   Is there a more "normal" way?
26898
I have this script to divide a file over a 100000 or even more in 50000 lines.  desc()                                                     {                                                       echo $1 $2|awk '{d=$1;}                                 BEGIN {a=1;b=0}                                            { f=d"0"a;                                                while ((getline x&lt;d)&gt;0)                                   { print x&gt;&gt;f".txt"                                        if (b++&gt;=50000)                                            {                                                         close(f);b=0;a++;f=(a&lt;10)?d"0"a:f=d""a                }                                                  }                                                   }                                                   END {print "Terminado ... ! "}'                         }                                                      if [ -f $1 ]; then                                              desc $1 $2                                      fi                                                        When I execute it I got this error message:  sh-3.2$ parte.sh pa.txt                                  : command not found                                      '/parte.sh: line 2: syntax error near unexpected token ` '/parte.sh: line 2: `desc()                              sh-3.2$                                                    Can anyone help to solve it?
103460
I have a text file I'm outputting to a variable in my shell script.  I only need the first 50 characters however.  I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters?  That may be due to cut looking for lines (not 100% sure), while this text file could be one long string-- it really depends.    Is there a utility out there I can pipe into to get the first X characters from a cat command?
86903
If I'm assigning a variable with  temp=$!   what would it be its value?
34813
I see this often in the build scripts of projects that use autotools (autoconf, automake).  When somebody wants to check the value of a shell variable, they frequently use this idiom:  if test "x$SHELL_VAR" = "xyes"; then ...   What is the advantage to this over simply checking the value like this:  if test $SHELL_VAR = "yes"; then ...   I figure there must be some reason that I see this so often, but I can't figure out what it is.
27528
I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.    Is there a function that can do this easily?  For example:  $string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing."; $string2 = 100charfunction($string1); print $string2   To get:  I am looking for a way to pull the first 100 characters from a string vari
145
Given a filename in the form someletters_12345_moreleters.ext, I want to extract the 5 digits and put them into a variable.  So to emphasize the point, I have a filename with x number of characters then a five digit sequence surrounded by a single underscore on either side then another set of x number of characters.  I want to take the 5 digit number and put that into a variable.  I am very interested in the number of different ways that this can be accomplished.
76806
Below is some sort of pseudo-code for what I'm trying to accomplish:  #!/bin/bash  # I already have the variable below figured out (positive integer): numlines=$([returns number of lines containing specific characters in a file])  # This is basically what I want to do with it: for i in {1..$numlines}; do     # the part below is already figured out as well:             do some other stuff done   I can execute it fine from the command line by inserting the actual number in the `{1..n}' sequence.  I just need to know if it's possible to include a variable here and how to go about doing it.   I have tried exporting it I have tried putting the variable itself in curly braces inside the sequence: {1..${numlines}} I have tried putting it in double-quotes hoping it would expand: {1.."$numlines"} I have tried escaping the $: {1..\$numlines}   Do I need to use a set -[something] command in order for this variable to get expanded?  I have even tried some forms of using eval...all to no avail.  I just need to know if there is something simple or obscure that I am missing or if this is even possible before I waste anymore time on it.  I could throw together a really, really hackish way of doing it to make it work as needed, but I'd like to avoid that if at all possible and learn the right way to go about doing it.
72555
In bash, say you have var=a.b.c., then:  $ IFS=. printf "%s\n" $var a.b.c   However, such a usage of IFS does take effect while creating an array:  $ IFS=. arr=($var) $ printf "%s\n" "${arr[@]}" a b c   This is very convenient, sure, but where is this documented? A quick reading of the sections on  or  in the Bash documentation does not give any indication either way. A search for IFS through  doesn't provide any hints about this effect either.  I'm not sure when I can reliably do:  IFS=x do something   And expect that IFS will affect field splitting.
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`
115440
Simply said, I just can't find a way to match a variable against some pattern containing spaces.  Here is what I expected to work (echo 'ok' string)  item='foobar baz' pat=".+bar baz" if [[ "$item" =~ "$pat" ]] ; then     echo ok fi   Adding/removing quotes around $pat does not seem to make any difference. I get those two erros below:  bash: conditional binary operator expected bash: syntax error near `~='   Could someone please help me pointing out what I am doing wrong here ? Should I put the pattern right away (without any quotes/double quotes, nor variable reference) ? If that is the case, then how can I put spaces in ? (using reg-exp matching, not an alternative)  Thank you !
14687
Please explain to me why the very last echo statement is blank? I expect that XCODE is incremented in the while loop to a value of 1:  #!/bin/bash OUTPUT="name1 ip ip status" # normally output of another command with multi line output  if [ -z "$OUTPUT" ] then         echo "Status WARN: No messages from SMcli"         exit $STATE_WARNING else         echo "$OUTPUT"|while read NAME IP1 IP2 STATUS         do                 if [ "$STATUS" != "Optimal" ]                 then                         echo "CRIT: $NAME - $STATUS"                         echo $((++XCODE))                 else                         echo "OK: $NAME - $STATUS"                 fi         done fi  echo $XCODE   I've tried using the following statement instead of the ++XCODE method  XCODE=`expr $XCODE + 1`   and it too won't print outside of the while statement. I think I'm missing something about variable scope here, but the ol' man page isn't showing it to me.
25194
I am trying to pull logs and doing few others things directly. written a small line BASH but not working. I am sure thats with IF Condition.    sed -rne '/21:25:07/,/21:50:07/ p'  server.log.2015-04-21 &gt; /tmp/filename.log ; du -sh  /tmp/filename.log ;  if [`du -sh  /tmp/filename.log` -gt 0] then gzip /tmp/filename.log ;
1430
I want to check whether Value1 below contains "abc" within the first X characters. How would you check this with an if statement?  var Value1 = "ddabcgghh";  if (Value1.Contains("abc")) {     found = true; }   It could be within the first 3, 4 or 5 characters.
15222
After reading ilkkachu's answer to  I learned on the existence of the declare (with argument -n) shell built in.  help declare brings:     Set variable values and attributes.      Declare variables and give them attributes. If no NAMEs are given,   display the attributes and values of all variables.      -n ... make NAME a reference to the variable named by its value   I ask for a general explanation with an example regarding declare because I don't understand the man. I know what is a variable and expanding it but I still miss the man on declare (variable attribute?).  Maybe you'd like to explain this based on the code by ilkkachu in the answer:  #!/bin/bash function read_and_verify  {     read -p "Please enter value for '$1': " tmp1     read -p "Please repeat the value to verify: " tmp2     if [ "$tmp1" != "$tmp2" ]; then         echo "Values unmatched. Please try again."; return 2     else         declare -n ref="$1"         ref=$tmp1     fi }
51024
When I am running two while loop and trying to concatenating two variable strings (from two loops), the final output (string) is weird. I have tried different way to concatenating but did not get the answer: Below is the code: [I need vfile="var1_Amon_My_model1*.nc"]  #!/bin/bash jcount=1 extn="*.nc" while read line do mname=$line echo " Working on model - " $mname echo " and model number = "$jcount while read line do vname=$line vfile="${vname}""_Anom_""${mname}""${extn}" echo $vfile done&lt;varlists.txt echo "******************************************" jcount=$((jcount+1)) done&lt;model_test1.txt   In varlists.txt  var1 var2   In model_test1.txt  My_model1 My_model2   After executing the script, it is showing   Working on model -  My_model1  and model number = 1 *.ncm_My_model1 *.ncm_My_model1 ******************************************