New answers tagged

0

Copy sound ambiguously. In this case, it seems you want to get a output from a script and another command and append to a file (not copy the content of a file to another, right?). What you need is redirect the output to a file The basic syntax is: command --options arguments >> file.txt but, if you have several commands to run, you could create a group ...


1

If you can modify the script, you can make it append the output to another file automatically. Just add a { on a line by itself before the second echo, and then } >>arch.make after the last echo: #echo "Please add ..." { echo "" echo "INCFLAGS ..." # etc. echo "FPPFLAGS ..." echo "" } >>arch.make Running that would add the result of the echo ...


0

Assuming you use bash, there are many ways to do this as mentioned above. Here is what I found to be the most convenient - YMMV: Create a function for each "shortcut" that you want to use. Have all such functions in a file called, say, ~/.bash_aliases Source this file in your .bash_profile. Call the function followed by the argument(s). Note that, strictly ...


1

@muru Thanks for those leads. Using them, I came up with this one-liner: tail main_top.csv -n 1 | awk -F, '{print $NF}'


1

There's also the GLOBIGNORE variable: The GLOBIGNORE shell variable may be used to restrict the set of file names matching a pattern. If GLOBIGNORE is set, each matching file name that also matches one of the patterns in GLOBIGNORE is removed from the list of matches. If the nocaseglob option is set, the matching against the patterns in GLOBIGNORE ...


3

There is no difference whatsoever. The POSIX description of both options reads True if pathname resolves to an existing directory entry for a symbolic link. False if pathname cannot be resolved, or if pathname resolves to an existing directory entry for a file that is not a symbolic link. If the final component of pathname is a symbolic link, that ...


0

If you delete line N from the history, then line N+1 moves in position N etc. For this reason, I prefer identifying the oldest and newest history line between which I want to delete all history. (Note: oldest < newest). If for instance I want to delete the history lines from oldest = 123 up to newest = 135, I'd write: $ for i in {135..123}; do history -...


1

Not easy in bash, but it can be done using associative arrays. #! /bin/bash declare -A crypt=( [A]="99_banana" [a]="@_melon" [B]="22_GRAPE" [b]="orange" [C]="Strawberry" [c]="life" ['@']="12399" ['!']="tv_12" ) encode () { local word=$1 for ((i=0; i<${#word}; ++i)) ; do local char=${word:$i:1} ...


3

I have a habit of using find for this kind of thing. find . -type f -name "*.imputed.*_info" -exec awk '$5 >= 0.5' {} + >> ./snplist.txt


1

The easiest way to "export" the shell db variable to a program that the script runs would be to pass it as an argument, and then the python command can read it from sys.argv. It might look like this: while IFS= read -r db do printf "%s\n" "${db} Found" python -c 'import sys; print("db: %s" % sys.argv[1])' "$db" done < path/to/file.txt


0

Wish the question was a bit clearer. But it seems like, it is getting a bit complicated. How about trying something like this: oldIFS=$IFS; IFS=","; file="$1"; csv="$2"; name=$(basename -s .svg $1) while read -r id colour; do if [[ $( grep -E "<g id=${id}>" $file ) ]]; then sed -i "s/id='ca'/id=$id style="fill:red;"/' ${file}....


0

I would split your script into two: One to build, which doesn't require to be run as root. (your current install.sh that would be renamed build.sh) One to install, which requires to be run as root. (the real deploy.sh). And, why not a 3rd script that would be called install.sh and would do: call build.sh regulary. call sudo deploy.sh


2

You shouldn't need to read the CSV into variables, you can just loop on the CSV directly: cat data.csv | while IFS=, read id colour; do # something with $id and $colour Doing var=$(echo text) is kind of redundant - you should just use var="text" directly. I'm not sure what you mean by the construct [ "grep -E..." ], but that is basically testing if the ...


1

Possibly not ideal for your situation but I was wondering how about making a function in pure Bash and then passing the extension as an arg, which can be handy if you want to add it in your bashrc and you want to do the same thing in the future, like the following So, basically what we are doing is the regex check for your desired extension. for file in *; ...


2

You want to pipe the output of both commands as a group to the final script, not the output of echo to cat. So group those commands using { ...; } or (...): { echo 'use database'; cat create_tables.sql; } | work_send_command_to_db.sh Or: ( echo 'use database'; cat create_tables.sql ) | work_send_command_to_db.sh


2

the simplest way I can think of: find ${path-to-folder} -type f | grep -vE ".*\.mp3$" You find all files in a folder and pipe it to a reverse extended grep: Find all files in a location find ${path-to-folder} -type f Reverse grep and use a regex to filter by extension grep -vE ".*\.mp3$" Grep flags: -v reverse grep -E extended grep (use of regex) ...


9

*.!(mp3) matches on foo.bar.mp3 because that's foo. followed by bar.mp3 which is not mp3. You want !(*.mp3) here, which matches anything that doesn't end in .mp3. If you want to match files whose name contains at least one . (other than a leading one which would make them a hidden file) but don't end in .mp3, you could do !(*.mp3|!(*.*)).


1

Tack on two more expansions: $ printf "%s\n" {a..z}{a..z}{a..z} | head -n 5 aaa aab aac aad aae $ printf "%s\n" {a..z}{a..z}{a..z} | tail -n 5 zzv zzw zzx zzy zzz Bash supports padded expansion for numbers: $ printf "%s\n" {000..999} | head -n 5 000 001 002 003 004


2

for string in {A..Z}{A..Z}{A..Z}; do # use "$string" here done This loop would iterate 17576 times (26*26*26 times). To create all zero-filled number between 000 and 999, you may use {000..999} in bash release 4 or later. In earlier releases of bash, you may possibly want to use the same technique as is used with the loop above. Or, you could use ...


2

It should be as simple as set -e mysql -h localhost -D test -ppassword <<-RENAME || true SET FOREIGN_KEY_CHECKS=0; RENAME TABLE $table TO $table\_keep; SET FOREIGN_KEY_CHECKS=1; RENAME or you could put it in an if statement set -e if mysql -h localhost -D test -ppassword <<-RENAME SET FOREIGN_KEY_CHECKS=0; ...


0

To temporarily recover a normal terminal, you can use: kill -28 $$ You must put [ and ] around any non-printing escape sequences in your prompt. Thus: blue=$(tput setaf 4) purple=$(tput setaf 5) reset=$(tput sgr0) export PS1="\[$blue\]\h:\[$purple\]\w\[$reset\]\\$ " So stop using raw ansi codes, it's not readable, better use tput. Check https://mywiki....


13

Your code overwrites the output file in each iteration. You also do not actually call awk. What you want to do is something like awk '$5 >= 0.5' ./*.imputed.*_info >snplist.txt This would call awk with all your files at once, and it would go through them one by one, in the order that the shell expands the globbing pattern. If the 5th column of any ...


5

Just do this : awk '$5 >= .5' *.imputed.*_info > snplist.txt


1

There are so many errors it is hard to know where to start if this is really supposed to be shell. if prname == 'CheckForUpdate': in shell will call a command prname with two arguments == and CheckForUpdate: and so will probably give you an error saying it can't find a command named prname. if [ prname == 'CheckForUpdate' ] || [ prname == 'Result' ] is ...


1

(sleep 0; echo "twice") | tee >(dump "./a.txt") echo "$(< "a.txt")" IIUC, the question is how to wait for the process inside >(...) to finish before executing the $(...) command substitution from the next line. The answer is that there's no nice way to do that. If your system supports the /dev/fd/ mechanism, you could use an exec fd> >(...) ...


0

@mosvy's very helpful answer was mostly correct, but has the problem that b() always aborts the pipeline whether or not sed /die/q encounters "die": Input stream contains "die" $ b(){ sed /die/q && kill "$BASHPID"; }; printf '%s\n' pass die oops | b | cat; echo "${PIPESTATUS[@]}" pass die 0 143 0 Input stream does not contain "die" $ b(){ sed /...


2

The dump call in the process substitution is running as an asynchronous process. This means that tee writes its output to it, and then the pipelines finishes. The pipeline finishes because the output from tee gets buffered; if you wrote more data than the size of the pipe buffer, tee would have to wait for dump to consume it, and your original code would ...


0

Some versions of Bash have a bug such that they do not properly wait for processes spawned by process substitution.


3

Using a separate file descriptor for the read command is a good solution. You are guaranteed that file descriptors between 3 and 9 will be available to you for doing this. The shell will not use these for anything else. Only file descriptors 0, 1, and 2 have predefined uses, and you may find that using descriptors with values larger than 9 works, but there ...


1

| is not a command. So that is not possible. But you might be able to simulate what you want by using history and abusing $PS1: PS1="$PS1"'`(date;history | tail -n1 | perl -ne "s/\|.*// and print") >> ~/pipe.log`'


1

Although unlikely to be configured to operate in /tmp, the tool logrotate could truncate a file when performing its rotation. Also if a process writes to the file without an append flag, this would also cause it to truncate the file. In bash this is the difference between a single '>' and double '>>'. In other languages it's the 'a' mode instead of 'w' ...


0

My favorite way (in ~/.bashrc): function bassh() { local host=${1:?'arg #1 missing: remote host'} shift local command="$@" local usage="bassh REMOTE_HOST COMMAND" [ "$command" ] || { echo -e >&2 "[error] no command provided\nUsage: ${usage}" return 1 } ssh ${host} -t bash -ic "'${command}'" } Note the -t ...


0

In my Debian 9 I have ps that reports (i.e. ps --version reports) ps from procps-ng 3.3.12. The manual states: This ps supports AIX format descriptors, which work somewhat like the formatting codes of printf(1) and printf(3). For example, the normal default output can be produced with this: ps -eo "%p %y %x %c". It turns out I can include arbitrary ...


0

Using a combination of @rozcietrzewiacz's option 3 with an exit trap will enable terminals that maintain their own independent history sessions which converge on close. It even seems to work well with multiple sessions across different machines sharing a remote home directory. export HISTSIZE=5000 export HISTFILESIZE=5000 export HISTCONTROL=ignorespace:...


0

Using printf: $ domains=("gmail.com" "yahoo.com" "yahoo.co.uk" "yahoo.co.jp") $ s_domain=( $(printf ' 0%.0s' "${domains[@]}") } $ printf '%s ' "${s_domain[@]}"; echo 0 0 0 0


1

Simple bash script: $ cat script.sh !/bin/bash file1=$1 file2=$2 while IFS=';' read a b do r="$b" while IFS=';' read c d do if [ "$a" == "$c" ] then r="$d" fi done < $file2 printf "%s;%s\n" $a $r done < $file1 $ ./script.sh 11_19 12_19 id=123;123123 id=456;445566 id=789;778899 id=000;999999 $ ...


2

[Edit: I used data1 for OP's file 11_19.txt and data2 for OP's file 12_19.txt] Note: as shown data1 and data2 need to be sorted on their respective first column, prior to executing the JOIN on first column. $ cat data1 id=123;112233 id=456;445566 id=789;778899 id=000;000000 $ cat data2 id=123;123123 id=000;999999 Then issue either: $ sed -E 's/\;.*\;/;/...


0

This will rename all files in current directory that DO NOT already have a file extension.. Example: your directory has files named: file1, file2, file3. They will be renamed to file1.mp4, file2.mp4, file3.mp4 for file in *; do mv "$file" "${file}.mp4" done If there are files with a dot which must be excluded: for file in *; do [[ $file == *.* ]] &...


0

If you want to display the third line of a text file, something like: cat yourfile | head -n 3 | tail -n 1 should do the job. If you have all the values in one line, you may want to reorder them first echo 1 2 3 4 5 6 7 | sed -e "s# #\n#g" 1 2 3 4 5 6 7 There are many other solutions possible, this is just something which came to my mind first.


3

The shell isn't a good tool for this sort of thing. There are tools designed especially for this type of task: $ awk '{print $3}' file 5 Or, if this is a tab-separated file: $ cut -f3 file 5 If not, you can convert the consecutive spaces to tabs and still use cut: $ sed 's/ */\t/g' file | cut -f3 5 Or, convert them to single spaces and use cut -d' ': ...


0

You could use the following to display the third item in all lines of a file (assuming the text file is named numbers.txt): while read one two three four; do echo "$three"; done < numbers.txt


0

http://tldp.org/LDP/abs/html/io-redirection.html cat stuff | 3>&1 tee 3 | xsel -i Edit: unfortunately, the above ↑ is incorrect. An earlier answer of cat stuff | tee >(cat) | xsel -i is good though.


2

Looks almost OK. Try the following (untested obviously) #!/bin/bash #checks to see if programA is already running if pidof -x "programA" >/dev/null; then echo "Program A already running" exit 1 fi expect <<EOF spawn ssh username@${1} expect "assword:" send "password\r" expect "$ " send "nohup ./programB &\r" # run programB in ...


0

you may be inadvertently overwriting the character device /dev/ttyUSB0, making it essentially a regular file. do a file /dev/ttyUSB0 on it after failure to see what it is. Also, it seems like this is true as the group changes from dialout to root.


0

this will do that: https://github.com/christopher-barry/bash-color-tools run install, then run setup-prompt for each user. The prompt is theme-able, so you can set colors, have different stuff displayed, etc.


1

backticks `blah` are deprecated, and generally $(blah) should be used instead, but really, basename is not needed, just use parameter expansion, like so: printf '%s\n' "${userlistlocation##*/}"


0

You could use select: select car in "${array[@]}" exit; do case $car in exit) break;; *) printf 'Choosing car: %s\n' "$car" kubectl edit pod -n ns "$car" -o yaml ;; esac done of course this would depend on you properly setting the array first by using the array=() syntax: array=($(kubectl get pods -n ...


0

Maybe something like can help to generate the list: for (( i=1; i<=${#array[@]}; i++ )) do echo $i ${array[$i]} done


0

After doing a bit more research, it looks like I'm in luck given that I use zshell bash directory shortcuts Apparently you can export any variable eg export icloud="/Users/james/Library/Mobile Documents/com~apple~CloudDocs" and refer to it via ~icloud


0

Here's all the conversions using printf: printf "%o" "'J" # 112 (oct) printf "%d" "'J" # 74 (dec) printf "%x" "'J" # 4a (hex) printf '\112' # J (oct) printf "\x$(printf %x 74)" # J (dec, requires double conversion) printf '\x4a' # J (hex)


Top 50 recent answers are included