Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

1

That's because brace expansion happens before variable expansion. You can use seq instead: num=5 for i in $(seq 1 $num) ; do echo hello done


0

In bash to print any range of characters, not just alphabetic or numeric: $ printf '%b\n' "$(eval printf '\\%03o' $(printf '{%d..%d}' "'!" "'~"))" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~


1

With bash using paste, head, wc and printf: # generate somefile $ printf 'line %s of file\n' {1..5} > somefile # then paste $ paste -d ' ' <(printf '%s\n' {a..z}) somefile a line 1 of file b line 2 of file c line 3 of file d line 4 of file e line 5 of file f g ... As we don't know how many characters we need to generate (a-e in this example), we ...


3

You can use awk: awk '{printf "%c\t%s\n", NR+96, $0}' (97 being the ASCII decimal value of a) % seq 1 10 | awk '{printf "%c\t%s\n", NR+96, $0}' a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10


2

Short answer; it can't be done, and for good reason! If that were so, than anyone could become root. Think about it. :) HTH


-1

In all honesty. The correct escape method depends largely upon the shell used. The most common escape (as you already mentioned) is simply a backslash \. But, depending on it's usage, and the shell you use. The method may be different. So. In conclusion. The correct answer can only be given if the shell you're using is provided. :)


0

I was using Notepad++ on Windows so I had to run sed -i -e 's/\r$//' /etc/start.sh to replace the Windows line endings. The ^M is a carriage return character. Linux uses the line feed character to mark the end of a line, whereas Windows uses the two-character sequence CR LF. Your file has Windows line endings, which is confusing Linux. Answer-...


2

You should not export the functions. Instead I suggest to source .bash_aliases for all bash instances that need it. (I guess you need the functions in interactive bash shells only.) A similar problem is mentioned here: /bin/sh: error importing function definition for `some-function', where the accepted answer proposes to rename the functions.


2

The simple solution is to not source them. Since ~/.bash_aliases isn't a standard file, you must be explicitly sourcing it in one of your config files. So an easy solution would be to find the line that source it (most likely in your ~/.bashrc) and change it from this (or whatever your system has, this one is from Ubuntu): if [ -f ~/.bash_aliases ]; then ...


4

diff is a relatively expensive command, at least if the files are different. Calculating a minimal set of changes is (relatively) CPU intensive. So its understandable not to want to do that twice. cmp, however, is cheap, CPU-wise. Presuming these files are of a reasonable size (I doubt you'd call diff on multi-GB files), it will have almost no performance ...


8

You could call the command once, redirect the output, then remove the output if there were no differences: diff a c > output.txt && rm output.txt


0

Distribution is good: for (( i = 1 ; i <= 100000 ; i++ )) do echo $(( RANDOM % (20 - 10 + 1 ) + 10 )) ; done | sort -n | uniq -c count value 9183 10 9109 11 8915 12 9037 13 9100 14 9138 15 9125 16 9261 17 9088 18 8996 19 9048 20


5

What about temporary file? diff a c > /tmp/output.txt if [ $? != 0 ]; then mv /tmp/output.txt /my/folder/output.txt; else rm -f /tmp/output.txt; fi replace the -f with -i if you want delete confirmation dialog. This way you only run command twice, no temporary variables and no 'middleman' be it echo, printf or anything else.


1

It's your use of double quotes. You cannot use double quotes "inside" double quotes unless they are escaped. Instead you could either escape your double quotes or more preferrably alternate quotes by using single quotes instead: #!/usr/bin/perl use warnings; use strict; exec("sed -i -r '4{s/\{\+//; s/\+}//; s/\[-.*-]//g}' error.txt");


1

You should quote the variable in your echo command: $ var=? $ echo "$var" ?


0

Use below command find path -type f -iname "filename" -exec cat {} \;


0

I am not 100% what you want, but consider the following: grep -Rl pattern | xargs cat grep -l will print only the file location relative to your current working directory, while xargs will redirect it to cat thus instantly printing the content of the file(s) found.


0

I have added: # Start tmux if tmux is not already running pgrep tmux >/dev/null 2>&1 || exec tmux at the end of my .bashrc and it looks to me that everything is working fine. When I start a gnome-terminal manually (for the first time), tmux starts. If I open other terminals, they won't have tmux (and this is pretty much what I want). And opening ...


0

Presumably you are using something like the query described in XTerm Control Sequences: OSC Ps ; Pt BEL OSC Ps ; Pt ST Ps = 4 ; c ; spec -> Change Color Number c to the color specified by spec. This can be a name or RGB specification as per XParseColor. Any number of c/spec pairs may be given. The color numbers correspond to ...


18

[this should've been a comment, but my comments tend to be deleted by moderators, so this will stay as an answer that I could use it as a reference even if deleted] Using BASH_SUBSHELL is completely unreliable as it be only set to 1 in some subshells, not in all subshells. $ (echo $BASH_SUBSHELL) 1 $ echo $BASH_SUBSHELL | cat 0 Before claiming that the ...


41

In bash, you can compare $BASHPID to $$ $ ( if [ "$$" -eq "$BASHPID" ]; then echo not subshell; else echo subshell; fi ) subshell $ if [ "$$" -eq "$BASHPID" ]; then echo not subshell; else echo subshell; fi not subshell If you're not in bash, $$ should remain the same in a subshell, so you'd need some other way of getting your actual process ID. One way ...


38

How about BASH_SUBSHELL? BASH_SUBSHELL       Incremented by one within each subshell or subshell environment when the shell       begins executing in that environment. The initial value is 0. $ echo $BASH_SUBSHELL 0 $ (echo $BASH_SUBSHELL) 1


2

You cannot "upgrade" an already running shell. You can however a) create a pty and run another shell in it with script /dev/null b) fiddle with your local terminal so it doesn't intepret the intr, eof, eol and other keys specially, but pass them through. $ nc -lvp 9999 Listening on [0.0.0.0] (family 0, port 9999) [ncat -4 localhost 9999 -e /bin/bash ...


0

The following function computes the number of necessary digits (1 for up to 9 files, 2 for up to 99 files, a.s.o.) and renames the files with the first number in the filename padded with zeros accordingly: pad_numbers_with_zeros(){ folder="$1" [[ -d "$folder" ]] || { echo 'need directory argument' && return; } extension="$2" [[ -z "$...


1

As @panki said It was simple: $> fn(){DATA=$(sed -n ''"$2"'p' $1 | trans -no-init -no-warn -b -t ru) && \ sed -i ''"$2"'a '"$DATA"'' $1} && \ fn file_for_transalate.md 82


0

Tried with below command and it worked fine awk 'NR==2{print $NF}' filename| awk -F "|" '{$1="";print $0}' |sed -r "s/^\s+//g"| perl -pne "s/ /\n/g" output papa salchicha aceite queso


0

You could read them all into an array by changing IFS and keep them for future use..... OIFS=$IFS; IFS='|'; array=($(sed '2q;d' "$filename")); IFS=$OIFS echo ${array[0]} Salchipapa echo ${array[1]} papa That way you are not restricting yourself to an arbitrary 10 values and you also don't need to test for 'null' later because you know the number of ...


0

Assuming that the actual file's content is 31915 Salchipapa|papa|salchicha|aceite|queso then you could use awk -F '|' 'FNR == 2 { for (i=2; i<=NF; ++i) print $i }' file to print the fields from the 2nd field onwards from the second line, one word per line of output. The following sed command would do the same: sed '1d;2y/|/\n/;s/[^\n]*\n//' file It ...


0

I did it like this and it worked: for i in {2..10} do ingName=$(sed -n 2p $filename | cut -d '|' -f $i) done "sed -n 2p $filename" gets the second line of the file and then "cut -d '|' -f $i" gives me each ingredient separated by pipes. the number 10 indicates that there are maximum 10 ingredients, so later on in the code I validate ...


0

Just create a new Dockerfile, and add your customizations there. FROM alexcheng/magento2:latest RUN apt-get update \ && apt-get install -y zsh && \ usermod -s /usr/bin/zsh www-data Then to build it, docker build -t alexcheng/magento2:latest-zsh .


-1

Creating a one-liner: $ c () { echo $(( ${1} )) } Now you can use simple integer math: $ c 1+1 2 $ c 25*4 100 $ c 25*4-10 90 $ c 20*5/4 25


2

gdb -p PID -batch -ex 'p fflush(stdout)' As with any debugging and hacking, YMMV.


2

Do you have access to the source of the running programs? Forcing a flush of an arbitrary executable is, while not theoretically impossible, very difficult. You would need to find the fflush function in the code and the stdout argument, then interrupt the execution of the program, arrange for the call to fflush, then continue execution. If the program is ...


-2

This is because the disk space is not enough, you need to clean up large files or clean up the process that takes up space: df -h View hard disk space du -sh /* View which directory is the largest, step by step to find large files du -h --max-depth=1 find the largest file


0

I'm running the Ubuntu as WSL subsystem on Windows 10 190318. Running sudo updatedb did the trick for me. Kudos to https://jaimyn.com.au/possible-fix-really-slow-tab-auto-completion-ubuntu-bash/


1

xargs expects a very special input format where arguments are delimited by blanks or newlines (sometimes other forms of vertical whitespace, sometimes dependant on the current locale), and where single quote, double quotes and backslash can be used to escape them (but in a different way from shell quotes). -l1 is not to pass one line of input as one single ...


1

While it is unlikely to be 'malicious' attack,that PATH is something you should be concerned about. Let's consider the directories in that PATH: /home/uname/bin /home/uname/.local/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games I have two comments: Because /home/uname/bin and home/uname/.local/bin are the ...


-1

wdir="$PWD"; [ "$PWD" = "/" ] && wdir="" case "$0" in /*) scriptdir="${0%/*}";; *) scriptdir="$wdir/${0#./}"; scriptdir="${scriptdir%/*}";; esac echo "$scriptdir" Maybe this will not work with symlinks or sourced files but work for normal files. Taken as reference fro. @kenorb No dirname, readlink, BASH_SOURCE.


6

mv1 *.png first expands the wildcard pattern *.png into the list of matching file names, then passes that list of file names to the function. Then, inside the function $1 means: take the first argument to the function, split it where it contains whitespace, and replace any of the whitespace-separated parts that contain wildcard characters and match at least ...


3

$1 is the first argument to the function, here the first file that matches *.png. I guess that "$@" is what you want to use instead of $1.


-2

You would have to use mv1 \*.png. When interacting with functions, the Linux Terminal does not pass the asterisk directly to the command, but selects the first matching parameter and passes that one to the command. To allow the asterisk to pass through directly, one needs to escape the asterisk using a backslash.


3

There are a couple of issues here. Mainly, you iterate two loops, one inside fix.sh (with the while loop) and again recursively by calling back to dummy.sh. Also, you reset the counter file back to zero at the start of fix.sh, so it is never able to count up. You also have the ordering backwards, so the (recursive) calls to dummy.sh occur before you ...


6

For bash: Using the properties of regex you can write start with ^ and contain by nothing. The list of regexes to check start with aa abc or 3@3 and contains > is: ^aa ^abc ^3@3 > Make that a properly quoted list and ask bash to use regexes (=~): check_func() { matched=1 for test_regex in '^aa' '^abc' '^3@3' '>'; ...


5

Revised based on clarification to the question: This is less elegant (and much less flexible), but more compact than the other answers, check_func() { case "$1" in ( aa* | abc* | 3@3* | *">"*) return 0 esac return 1 } This returns true for aardvark, abcdef, 3@3com.com and 12>5.  And, of course, ...


4

Here's what the case statement does: take the second parameter to the function ($2). If it matches the pattern "$1"*, i.e. the first argument to the function followed by anything, then execute true and end the case statement. true does nothing and returns the status 0. Otherwise, if it matches *, i.e. anything, execute false and end the case statement. false ...


10

check_prefixes () { value=$1 for prefix in aa abc 3@3; do case $value in "$prefix"*) return 0 esac done return 1 } check_contains_gt () { value=$1 case $value in *">"*) return 0 esac return 1 } var='aa>' if check_prefixes "$var" && check_contains_gt "$var"; then ...


0

You can use the case function to better control it. #!/bin/bash i=$(ps -eaf | grep -i mysql | sed '/^$/d' | wc -l); echo $i case $i in 0) echo "The mysql service is 'not running'.";; 1) echo "The mysql Service has 'failed'. You may need to restart the service";; 2) echo "The mysql service is 'running'. No problems detected.";; esac The first ...


1

The simple way to do this is to use a while read loop. The general format is: while read var1 var2 ... varN; do something; done < inputFile By default, each input line is split on whitespace, so this is already essentially what you need: while read location hostname ip; do printf "Ping of $hostname ($ip): " ping -c1 "$ip" &>/dev/null &...


0

this answer describes how lsof and a bit of grep magic is the only thing that seems to stand a chance of working for nested sourced files under tcsh: /usr/sbin/lsof +p $$ | grep -oE /.\*source_me.tcsh


4

[this answer is about asynchronous pipelines in scripts; for the deprecated &> bash operator and why you should always use >output 2>&1 instead, refer to obsolete and deprecated syntax] #! /bin/sh cat "${1:-/dev/stdin}" | ... & Here you have a pipeline running asynchronously (because terminated by &), started from a script, ie is ...


Top 50 recent answers are included