New answers tagged

-1

Below is code i=10 for ((j=1;j<=count_of_sequence;j++)); do echo $i; sleep 10;i=$(($i+1)); done output 10 11 12 13 14


0

A horrible solution: #!/bin/bash somevar=0 somefile=$(mktemp) echo "$somevar" > "$somefile" while true; do sleep 3; ((somevar++)); echo "$somevar" > "$somefile" ;done & while true; do sleep 1 avar="$(cat "$somefile")" echo "my var has value $avar" done Certainly NOT precise with the times and NOWHERE near realtime-ready...


3

In bash, the $SECONDS special variable is already incremented once every second like in ksh where the feature comes from. It's initialised to 0 at the time the shell starts (or from the value of the $SECONDS environment variable if it's there), but you can also set its value by hand. However note that bash's implementation (contrary to that of ksh93, mksh ...


2

It is best to do this with an associative array. e.g. declare -A array while IFS=: read key val; do array["$key"]="$val" done < input.txt for key in Total_Test_Cases_Count Total_Pass; do printf '%s=%s\n' "$key" "${array[$key]}" done Output: Total_Test_Cases_Count=25 Total_Pass=24 This will fail if any of the values contain the field separator (a ...


1

Using awk and eval: eval "$(awk -F: '{ print $1 "='\''" $2 "'\''" }' tmp)" The command inside $() parses summary.txt. With the -F option we tell awk to use : as field delimiter. It formats each line like this: Total_Pass='24' I had to escape the single quotes; that is why the code looks so ugly. eval is used to execute the formatted text just as you had ...


3

(Unless you are using zsh as your interactive shell...) The issue is that the double quotes will be taken as part of the pattern. The command that you are actually running would be the equivalent of find . -name '"Bik*"' You get this because the shell would perform word-splitting on the unquoted $a, splitting the string tho the two words -name and "Bik*". ...


0

You could use base64: $ VAR="1/ ,x" $ echo "$VAR" | base64 > f $ VAR=$(cat f | base64 -d) $ echo "${VAR}X" 1/ ,xX


1

In a similar way as $* inserts a character in a concatenation of arguments an un quoted expansion removes characters from an string and every removal breaks the string into separated arguments. $ var=1234567890 $ IFS=368 $ echo "$var" $var 1234567890 12 45 7 90 $ printf '<%s> ' "$var" $var <1234567890> <12> <45> <7> <90>


3

Programs commonly write error messages to the “standard error” I/O stream; “stderr” for short.  If you search for that term, you’ll gets millions of results; the short explanation is that stderr exists so error messages can and will go to the screen when the “standard output” (“stdout”) is redirected, as in a hypothetical command like umount /dev/sdb1 > ...


0

A case statement is pretty quick, the way Schaller said above. Otherwise if you prefer conditionals you could write something like: if [[ -n "${id}" ]]; then if [[ "${id}" == "s001" ]]; then echo "do something" elif [[ "${id}" == "s002" ]]; then echo "do something else" fi fi Note: the -n test is to verify that id is set so the ...


3

The typical way to approach this is with a case statement: case $id in (s001) do something ;; (s002) do something else do something additional ;; (s003) do something else entirely ;; (*) do something unexpected ;; esac There's a bit of syntax to reinforce here: I like to wrap the options in open- ...


4

Variables are not expanded in single quotes. Use this: sed "${Column},8d" myfile.txt > result.txt


2

For completeness this is how I ended up solving my issue which was a combination of Kusalananda's answer and Michael Homer's answer. function executeServerlessCommand() { if [ "$ENABLE_SLS_DEBUG_LOGGING" = "true" ]; then SLS_DEBUG='*' "$@" else "$@" fi } executeServerlessCommand sls create_domain -v ${AWS_PROFILE_FLAG:+"$AWS_PROFILE_FLAG"} --...


4

The shell grammar recognises assignment words at the lexing stage, long before parameter expansion occurs. Although you can have a parameter expand to a command, you can't have one expand to an assignment — it's just treated as a command at that point, even though it's textually the same (it's like you wrote "SLS_DEBUG=*" sls ...). This is an oddity of the ...


5

$SLS_DEBUG_TEXT is expanded too late, after the stage where the shell would otherwise treat its value as an assignment. The variable's value is therefore instead treated as a command. What you could do instead is to use env: SLS_DEBUG_TEXT='SLS_DEBUG=*' env "$SLS_DEBUG_TEXT" sls create_domain -v "$AWS_PROFILE_FLAG" --stage "$STAGE" Note the quoting ...


2

i=0 while IFS= read -r line do var[i++]="$line" done < filename Here var array variable stores all names. And can be accessed by printf '%s\n' "${var[0]}" printf '%s\n' "${var[1]}" ... ...


4

With mapfile: $ mapfile -t array < yourfile $ declare -p array # print array content declare -a array=([0]="UserName" [1]="UserName" [2]="UserName")


3

$ set -f # disable globbing $ arr=($(<file.csv)) $ set +f # enable globbing $ declare -p arr # print array content declare -a arr=([0]="UserName" [1]="UserName" [2]="UserName") arr=(…) create array $(<file.csv) read file file.csv (like $(cat file.csv)) Before the file is read, globbing should be disabled to prevent the ...


0

A command (like your echo) takes command line arguments which could be flags (-e in your case). Many commands (at least common Linux versions) understand -- (two hyphens) as "end of flags, whatever follows is regular arguments". So you can delete a file perversely named -r by rm -- -r. For displaying stuff, printf is more robust all around (if harder to use)...


5

It works, but running echo -e doesn't output anything in Bash unless both the posix and xpg_echo options are enabled, as the -e is then interpreted as an option: $ help echo echo: echo [-neE] [arg ...] Write arguments to the standard output. Display the ARGs, separated by a single space character and followed by a newline, on the standard ...


-2

aa=`curl -d '{"foo":$(echo $bb)}' -H "Cont ... <and so the same>` aa=`curl -d '{"foo":"$bb"}' -H "Cont ... <and so the same>` bash version 4.2 also you can read about eval


1

The single quotes stops the variable from being expanded by the shell. User double quotes instead. For values that does not need quoting in JSON: aa=$( curl -d "{\"foo\": $bb}" -H ... ) or, aa=$( curl -d '{"foo":'"$bb"'}' -H ... ) If the variable's value needs to be JSON encoded (might be needed for some strings), or if you want to let a JSON parser ...


3

You don't have copy them one by one, you can paste all the lines together and newlines will work as Enter. The reason that ( var1="myvar1" var2="myvar2" ) doesn't work is that because it's executed in a subshell. It would work if you printed contents of the variable before the final ): ( var1="myvar1" var2="myvar2" echo $var2 ) It's explained in ...


0

The parenthesis you used causes the enclosed commands to be executed by a subshell; hence once that shell exits, de variables are still not set in the current shell. I'm at a loss why you think those parenthesis would set those variables "in one go", or why it would be necessary to them them "in one go"; however you could place those assignments on one line ...


Top 50 recent answers are included