New answers tagged

1

As long as you want to exclude the entire line if there are double vowels, this should work: grep -i a file | \ grep -i e | \ grep -i i | \ grep -i o | \ grep -i u | \ grep -v -i '\([aeiou]\)\1'


1

The standard way to do this with most shells (and therefore most portable) is with backquote (`) and since the arg you have to ssh does not need quoting, but the result probably would you'd write: UPTIME="`ssh $linux_server cat /proc/uptime`" or you can be specific to bash (and a few other shells, but not as universal as backquote) by using the $() ...


0

Your last example is correct (assuming you use a shell like bash or ksh93 that understands [[ ... ]], but it seems that you do): if [[ "$ipvsrc" == "$ipvdst" ]]; then echo "Value OK"; fi The two variables that you compare contains strings, so quoting them and using == is correct. The reason you're getting no output is probably simply because the ...


10

You can trap the DEBUG signal: trap 'printf "\n"' DEBUG DEBUG trapped command printf "\n" will be run before the command is executed unlike PROMPT_COMMAND which will be run after the command is executed. You can add this to your ~/.bashrc to make it permanent. Example: $ abc No command 'abc' found, did you mean: .... $ trap 'printf "\n"' DEBUG $ abc ...


2

In your loop, there is a short window of time between the "stty echo" at the end of the loop and the "stty -echo" at the next iteration. Keyboard input received during this window will be echoed, even though no read command is waiting for it. If you don't want echoes, don't call "stty echo" 😉


0

Just in case anyone is interested in further studies and/or clarification: In a nearly POSIX compliant shell implemented a while ago the internal workings of the 'exec_program()' and the 'builtin_source()' functions are very exemplary. In those functions you see exactly what is the difference between them: https://github.com/rsenn/shish/blob/master/src/...


0

myvariable=$(yourmysterycommand | sed -e 's/id1=//; s/id5=.*//; s/"//g; s/id.=/./g; s/[[:blank:]]\+//g' | paste -sd' ') That's a one-liner, but reformatted with extra newlines and indentation for readability. $ echo "$myvariable" 172....


1

Thank everybody's help. I try different one, this is working for me! #!/bin/bash command="\"stop\"" while : do QUESTION=$(cat stt.txt) #stt,txt has command "stop" echo $QUESTION echo $command if [ "$QUESTION" = "$command" ]; then echo "You said $command"\! break fi done


0

#!/bin/bash command="stop" while : do QUESTION=$(cat stt.txt) #stt,txt has command "stop" echo $QUESTION echo $command if [ "$QUESTION" == "$command" ]; then echo "You said $command"\! break fi done I made a two changes to your script. All strings entered directly into scripts for use in variables should be quoted, otherwise bash will ...


5

There must not be any spaces between a variable name and the equation mark. When there are spaces, the variable name is interpreted as a command, in this case the command host is run with parameters = and the host name.


5

You can do this with an awk command most easily: your-command | awk -F\" -v OFS=. -v ORS=' ' '{print $2, $4, $6, $8}' To set it as a variable, use command substitution: myVariable="$(your-command | awk -F\" -v OFS=. -v ORS=' ' '{print $2, $4, $6, $8}')" -F sets the input field separator (which is by default any whitespace) to a custom value; in this ...


0

If you want to specify the relevant column numbers by hand and then just use awk to print out two sums at the end, you can do: awk 'NR == 2 || NR == 3 || ... {sum1 += $1} NR == 1 || NR == 11 || ... {sum2 += $1} END {print sum1; print sum2}' file


2

There's no need to escape it if you just want an exclamation mark in the prompt. PS1='foobar!' See what happens.


0

bash has C-style for loops, but stylistically they're a little odd to use when you don't have to. And if you really do absolutely have to, you might be using the wrong language. (For this use case shell is fine, of course.) A much more shell-like way to do this (in my opinion) is: for i in {0..5}; do for j in $(seq "$i" 5); do echo "$i$j"; done; done ...


0

GNU Parallel will do the quoting of the argument correctly for you: tail -f logfile.log | grep 'patternline' | parallel bash scriptname.sh On top of that it will default to 1 process per CPU core and it will make sure the output of two parallel jobs will not be mixed. GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the ...


1

Maybe you are looking for a "chroot jail for ssh", if the users require a terminal. Otherwise, if you just need they can access to their homes, configure sshd to jail stfp users in their homes: add to sshd_config: Match group myGroup //Also can match users ChrootDirectory %h ForceCommand internal-sftp -u 0007 AllowTcpForwarding no ...


3

Your command $ find . -name 'segment*' | xargs -n1 -P4 sh someFunction.sh has the effect that at most four copies of the someFunction.sh shell script will be started (-P 4) in parallel (new ones will be spawed as soon as the old ones are done), each one getting one filename as its argument (-n 1). This means that each invocation of your script will look ...


2

You could use something like this assuming someFunction.sh is in your working directory. find . -name 'segment*' -print0| xargs -0 -n1 -P4 ./someFunction.sh The -print0 and -0 allow for files with spaces in the name (A common problem). In my someFunction.sh I have #!/bin/bash echo "Arg: " $1 cat $1 Which simply echo's out the file name then ...


1

for ((i = 0; i <= 5; i++)); do for ((j = i; j <= 5; j++)); do echo -e "${i}${j}\n" >> numbers.txt done done Should do the trick. I removed the space, I prefer using {}'s around my variables and \n puts in new line.


0

There is also the findmnt command, which can print the number of bytes or a "human" number (powers of 1024 with non-iso abbreviations, sadly): $ findmnt -no size /mnt/xyz 9.7G $ findmnt -bno size /mnt/xyz 10434699264


3

You can do it without the grep: df --output=target,size /mnt/xyz | awk ' NR==2 { print $2 } ' df accepts as argument the mount point; you can tell to awk too to print both the second line only (NR==2) , and the 2nd argument, $2. Or better yet, cut the target as you are not outputting it, and it becomes: df --output=size /mnt/xyz | awk ' NR==2 ' When I ...


1

You can do that with bash itself, using command substitution and then parameter expansion. First take the output of the command in a variable by using command substitution $(), and then use parameter expansion to replace all newlines with spaces ${variable//$'\n'/ }: $ myVariable=$(grep "type" /root/myFile | cut-d'=' -f2) $ myVariable=${myVariable//$'\n'/ ...


1

myVariable=`grep "type" /root/myFile | cut-d'=' -f2` What is between back-ticks (`) is run and the output is assigned to myVariable. If your current output is separated by line feeds (\n), then you may want to replace them with spaces with tr such as: myVariable=`grep "type" /root/myFile | cut-d'=' -f2`|tr '\n' ' '` Note: Some people prefer using the $...


0

A "clean" bash environment may be had with $ env -i bash --noprofile --norc The env -i command executes the command given to it on the command line without transferring any of the exported environment variables of the old shell environment to the environment of the executed program. The --noprofile option stops bash from reading the system-wide or ...


0

While the accepted answer is correct, what you usually want to do is to: env -i bash -l -c "printenv; and any other commands" This gives you bare but functional bash (same as you'd get when login in non-interactive mode). This for example sets the language, timezone, HOME, etc.


4

The alias provides a literal expansion. So running explorer / maps to pcmanfm 1>/dev/null 2>&1 & / This runs pcmanfm in the background, immediately followed by / in the foreground.


14

The way you wrote your alias, the command you run would be expanded as pcmanfm 1>/dev/null 2>&1 & '/' This will run pcmanfm without any options as a background job and then try to run / as a command. You probably want a function instead of an alias explorer() { pcmanfm "$@" >/dev/null 2>&1 & }


1

If you go a few levels deeper then it'll start truncating $ sweh in ~: cd D1 $ sweh in ~/D1: cd D2 $ sweh in ~/D1/D2: cd D3 $ sweh in ~/D1/D2/D3: cd D4 $ sweh in ~/D1/D2/D3/D4: cd D5 $ sweh in ~/.../D3/D4/D5: cd D6 $ sweh in ~/.../D4/D5/D6: Outside of $HOME it appears to truncates earlier: $ sweh in ~: cd /usr/local/share/locale/ $ sweh in .../local/...


0

Here is a complete tutorial. Lets have an example of script called admin.sh to which you would like to have autocomplete working. #!/bin/bash while [ $# -gt 0 ]; do arg=$1 case $arg in option_1) # do_option_1 ;; option_2) # do_option_1 ;; shortlist) echo option_1 option_2 shortlist ;; *) echo Wrong ...


0

Check the --output-delimiter option of cut, i.e. cut -f 1 -d ' ' --output-delimiter='\n' in your case


3

You can use history expansion $ echo test !#:^ echo test test test test $ echo a/b/test.py proj_copy/!#:^ echo a/b/test.py proj_copy/a/b/test.py a/b/test.py proj_copy/a/b/test.py !# The entire command line typed so far. :^ The first argument You could also use brace expansion $echo test{,} test test $echo {,proj_copy}/a/b/test.py /a/b/...


1

Apparently, Puppy Linux does not require sudo because "Puppy Linux typically is a single user OS. The single user is run as the root account with full privileges." So execute the command without sudo. However, you might require that if you are using fido (read the page pointed above).


1

You need to add a command to the relevant pane. If your default shell is bash, just have one pane run fish: [layouts] [[default]] [[[child0]]] fullscreen = False last_active_window = True maximised = True order = 0 parent = "" size = 1280, 985 type = Window [[[child1]]] order = 0 parent = ...


2

You were on the right track to use awk. You should write a script that reads your logs, and outputs with the fields separated with tabs¹. Then use the column command to re-align the columns: extract.awk²: BEGIN {OFS="\t"; print "Timestamp\tEmailTo:\tEmailFrom:\tIPAddress:\tErrorCodes:"} {print $1, $6, $7, $NF, $(NF-5)} Then run it with this command: awk ...


4

bash supports C-style for loops as follows: a=5 # example for ((i = 0; i < a; i++)); do for ((j = i; j < a; j++)); do echo "$i $j" done done See here for more: http://www.tldp.org/LDP/abs/html/loops1.html


1

OK, I found the answer in How to add pattern to bash completion (for unzip)? Executive summary: "-G" is pretty much useless. The best way to do it is to use "-f" which matches all files, and then -X '!*.cc' to exclude the ones I don't want. And then "add -o plusdirs" for good measure.


1

The bindings (whether they appear in the manual or not) appear when you type bind -p For instance (partial listing): "\C-g": abort "\C-x\C-g": abort "\e\C-g": abort "\C-j": accept-line "\C-m": accept-line # alias-expand-line (not bound) # arrow-key-prefix (not bound) # backward-byte (not bound) "\C-b": backward-char # backward-byte (not bound) "\C-b": ...


3

Add the following line to your ~/.inputrc file: set mark-symlinked-directories on See "Readline Init File Syntax" in the Bash Reference Manual for more on this topic.


3

The wc (word-count) utility is able to count lines in a file: $ wc -l num.txt ... or rather, it counts the number of newlines in the file, which most of the time is the same thing (actually, on a Unix system, that is defined as the same thing). The manual (on Mac OS X) states: "Characters beyond the final <newline> character will not be ...


4

Assuming there's a file called dates containing the list of dates, one per line (and nothing else), something like this might work to count the ones older than 14 days: $ date=$(date --date="14 days ago" +%Y%m%d) $ awk '($0 < "'$date'") {count += 1} END {print count}' < dates 20 (Given they are in yyyymmdd format, the comparison is easy.)


0

As is pointed out in section "1.3 Readline Init File" of the manual you refer to, the readline library is configurable. Keybindings may be defined either in /etc/inputrc, or in your local ~/.inputrc.


1

After considering other options presented here, and not fully understanding how some of them worked I developed my own path_remove function, which I added to my .bashrc: function path_remove { # Delete path by parts so we can never accidentally remove sub paths PATH=${PATH//":$1:"/":"} # delete any instances in the middle PATH=${PATH/#"$1:"/} # delete ...


0

according to the Bash-scripting guide. Process ID (PID) of the script itself. The $$ variable often finds use in scripts to construct "unique" temp file names. if you run echo $$ in a script you see the output is different from PID of current shell.


3

Your second command works, the issue is you are using bash or whatever shell that put all pipelines components in a subshell. myresult2 is properly set but the variable is immediately out of scope unless you stay in the same subshell like here: curl -L 'https://archive.org/wayback/available?url=stackoverflow.com' \ 2>/dev/null | { myresult2=$(...


1

From Advanced Bash-Scripting Guide: $$ is the process ID (PID) of the script itself. $BASHPID is the process ID of the current instance of Bash. This is not the same as the $$ variable, but it often gives the same result.


5

$$ is the process ID of the current shell instance. So in your case the number, 23019, is the PID of that instance of bash. The following should give you a better idea: ps -p $$


2

As user @muru says, it's not possible to do because you have already left the shell session behind when you get to the #!-line. However, depending on what your shell files do, there might be another solution. I'm guessing that they set environment variables that you use for some project. Let's call a project subtool (because that's a project I have). Then ...


1

Much easier way would be to disable hashing of your known_hosts with HashKnownHosts no in your ~/.ssh/config to allow autocomplete from all history, or just list your hosts in your ~/.ssh/config (you can store there all your Ports and Users, or create aliases). I appreciate the creativity, but why to reinvent wheel when we have got already the same ...


6

No. By the time a shebang comes into play, you have already lost. A shebang is applied when a process is exec()'d and typically that happens after forking, so you're already in a separate process. It's not the shell that reads the shebang, it's the kernel.


15

Bash is an interpreter; it accepts input and does whatever it wants to. It doesn't need to heed the executable bit. In fact, Bash is portable, and can run on operating systems and filesystems that don't have any concept of an executable bit. What does care about the executable bit is the operating system kernel. When the Linux kernel performs an exec, ...



Top 50 recent answers are included