Tag Info

New answers tagged

1

A few random hints depending on the actual data formatting and other issues... How are the data fields separated? (The first three spacings give the impression that there's a TAB character in between, while the last columns seem space separated.) You should be aware that the column information is lost for the culumns 4-N if your field separator is defined ...


0

bash does read ~/.bashrc though even when non-interactive when invoked over ssh (a misfeature IMO, but would come handy to you here). So you could add to the top of ~/.bashrc on the remote host: if [ -n "$SSH_CLIENT" ] && [ "$SHLVL" = 0 ] && [ -n "${-##*[il]*}" ]; then . /etc/profile . ~/.bash_profile fi


2

The semicolon ; is the sequencing operator. So in command1 & command2 ; command3 or equivalently command1 & command2 command3 command3 will run after command2 while command1 may still be running. The command wait will wait for all background processes (command1 in your example) to complete.


1

One approach would be to count the options as getopts parses them. Then, you can exit if less than a given number were passed: #!/usr/bin/env bash blastfile= comparefile= referencegenome= referenceCDS= help=''' USAGE: sh lincRNA_pipeline.sh -c </path/to/cuffcompare_output file> -g </path/to/reference genome file> -r ...


1

The [[ construct doesn't exist in all sh variants. It's a ksh thing that was adopted by bash and zsh. FreeBSD's sh is an ash derivative and doesn't support [[. If you want to use [[ in a script, use a shebang line that calls a shell that supports [[. You can install bash or ksh93 or mksh as a package on FreeBSD; all of them support [[. Packages are ...


1

You need to direct both stderr and stdout into the file: nc -vv -z localhost 1-80 > file.txt 2>&1


5

[[ is a bashism. /bin/sh is not guaranted to be the Bourne Again shell. Even on Linux operating systems, it could be the Debian Almquist shell, or the Policy-Compliant Ordinary shell. On the BSDs, it is not the Bourne Again shell out of the box because on the BSDs the Bourne Again shell is an optional add-on to the operating system proper. It's in the ...


2

The [[ syntax is a ksh and bash thing and is not present in all shells. Your FreeBSD default shell is probably sh (or bash acting like sh), not bash. The equivalent syntax that should work on all shells is: case $1 in *[/\\] ) echo "Yes";; esac


1

In addition to making sure the script is in the $PATH, you also must make the script executable. chmod +x SCRIPTNAME is how you do that.


1

You can check what locations are currently checked for direct commands by looking at the $PATH variable: echo $PATH It's likely this includes /usr/local/bin, in which case you could put a symbolic link there: ln -s /opt/mysuperscript /usr/local/bin/mysuperscript Now you can just type mysuperscript to run your script.


0

You have to install (copy/symbolic link) the script in one of the directories of $PATH or append the script directory to $PATH


0

Use GNU Parallel to parallelize your collection: parallel --slf rhel-nodes --tag --timeout 1000% --onall --retries 3 \ "rpm -q {}; rpm --queryformat '%{installtime:date} %{name}\n' -q {}" \ ::: bash bc perl Put the nodes in ~/.parallel/rhel-nodes. --tag will prepend the output with the name of the node. --timeout 1000% says that if a command takes 10 ...


3

The diagnostic "unexpected end of file" is a hint that you have some unmatched or unterminated opening syntactical construct (if w/o fi, do w/o done, opening brackets w/o the associated closing one, opening but unterminated quotes, etc.). The line number pointing to the end of the script is not helpful in this case, beyond saying to inspect your syntactical ...


-2

Try this: IFS=$(echo -en "\n\b") touch "$yourfile"


0

Try this: $ spam="foo bar" $ touch "$spam" $ ls -l -rw-rw-r-- 1 user user 0 Mar 29 05:14 foo bar


0

this is a great help to me. i had a need to create a set of subdirectories in multiple folders and something like: !/bin/sh cd /dest/cont for dir in */*/; do mkdir -p -- $dir/{FB,Video,Audio,proj}; done worked great except when a white space is encountered. is there a way to ignore spaces in the */*/ part of the arguments?


0

Thank you for your attempts to solve this. I have resolved the issue now, and the resolution needed was so simple, that I feel quite stupid now. In the code I had used previously, (ssh -q -o Batchmode=yes -o PasswordAuthentication=no -o ConnectTimeout=1 $i "rpm --queryformat '%{installtime:date} %{name}\n' -q \"kexec-tools\" | cut -d \" \" -f1,2,3,4|tr ...


6

With sed: sed 's/@.*//; s/[-_.]/ /g; s/\<./\U&/g' <<END user.name@domainname.com user.name2@domainname.com this.is.a.very.long.email.id@domainname.com END User Name User Name2 This Is A Very Long Email Id Requires GNU sed for the \U upper case directive. https://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command Expanded: ...


9

You can actually do this entirely in Bash, without any external commands, using word splitting and parameter expansion. It's even fairly short: EMAIL_ADDRESS=this.is.a.very-long_email.id@domainname.com USER=${EMAIL_ADDRESS%@*} WORDS=( $(IFS=._- ; printf '%s ' $USER) ) echo "${WORDS[@]^}" I'll take this line-by-line: USER=${EMAIL_ADDRESS%@*} This sets ...


0

Should be \! name "RCS" and it works best on the first check in. sudo find ~/.his/etc \! -name "RCS" -ls sudo find -d ~/.his/etc \! -name "RCS" | xargs sudo ci -l -mir -t-ir This last one also works very well for the cjeck in part filtering all RCS content on the file name: sudo find -d ~/.his/etc -type f | grep -v RCS | xargs sudo ci -l -mir -t-ir


0

This is a good one from the old Unix Masters (tested on FreeBSD 10.1) if you want to version control (with RCS check in locked: ci -l with a brief comment as ir: for initial release) your /etc/* config files on ~/.his/etc sudo rsync -av /etc/ ~/.his sudo find -d ~/.his/etc -type d -exec echo '{}/RCS' \; | xargs sudo mkdir -p sudo find -d ~/.his/etc \! ...


4

You can access the array index using ${!array[@]} and the length of the array using ${#array[@]} e.g. #!/bin/bash array=( item1 item2 item3 ) for index in ${!array[@]}; do echo $index/${#array[@]} done Note that since bash arrays are zero indexed, you will actually get 0/3 1/3 2/3 If you want the count to run from 1 you can replace $index by ...


1

Use PROMPT_COMMAND. What you're trying to do is exactly what it's for. It's expanded before the prompt is evaluated. If you want to derive some of the prompt content from that code, set variables in PROMPT_COMMAND, turn on the promptvars variable, and include these variables in PS1. See Stateful bash function and Display Non-Zero Return Status in PS1 and ...


3

First, let's look at what we see in the terminal: $ echo <(vim) /dev/fd/63 $ Vim: Warning: Output is not to a terminal Notice that you get a prompt back immediately, without waiting for the editor to terminate. Process substitution doesn't wait for the command to finish, it creates a pipe between the command and the shell. A name for that pipe is ...


2

stdbuf examples at http://www.pixelbeat.org/programming/stdio_buffering/ nohup is used for any long running command that you want left running across logins. You can also do this with screen(1) or retroactively with screen + https://github.com/nelhage/reptyr


2

Shells treat strings representing integers in decimal as integers. If you have a directory whose name contains only digits with no leading zeros, you have a number and you can perform arithmetic on it. for d in 1*; do mv "$d/old" "$((d+1))/new" done You can make the script more robust and only perform the move if the old subdirectory actually exists, ...


3

From the bash documentation: Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) (...) \nnn the eight-bit character whose value is the octal ...


0

The problem is that you keep adding to the indexes array, and never reset that. While you reset those variables to zero, set the array to be empty: totalentries=0 totalusedbytes=0 indexes=() Or, populate the array in a different way: mapfile -t indexes <<< "$indexlist"


2

You can pipe output via following script | while read a b ; do [ $a -gt 27 ] && echo "$a $b" ; done or | while read ; do [ ${REPLY% *} -gt 27 ] && echo "$REPLY" ; done but easyest through awk | awk '$1 > 27'


3

Assuming you mean the byte offset, from man od -A, --address-radix=RADIX output format for file offsets. RADIX is one of [doxn], for Decimal, Octal, Hex or None so for example od -An file


0

[[ "$1" -eq 0 ]] && { echo "Parameter 1 is empty" ; exit 1; }


1

Maybe you can use basename and or substrings? See man basename and http://tldp.org/LDP/abs/html/string-manipulation.html for details on the substrings. Using basename you can get the first portion of the filename ie. basename $i ".backup" will strip .backup from the filename. And ${i::-7} wills strip the last 7 characters from the variable.


2

I think you missed out an echo before the $i for i in $(find /directory -type f -name '*.backup') do echo $i $(echo "$i"|cut -d"." -f1-3) done`


-1

Solved it in two loops. May be it's not optimal, but works. If anybody know other solution - let us know. for i in $(find /directory -type f -name '*.backup') do for j in $(find /directory -type f -name '*.backup'|cut -d"." -f1-3) do echo $i $j done done


2

Two things you need to do: you don't need backticks around your invocation of mosquitto_pub your -t parameter to mosquitto_pub should be surrounded with double-quotes (") rather than single-quotes (") In particular (2) here is the cause of your problem. Single-quotes will not permit the evaluation of $ variables. if ... then #change ...


0

You can reference a function's arguments as numbered expansions in order of their appearance on the command-line when the function was called. If you wish to require that an argument is present in order for a function to operate, you can do this via an intrinsic failure-reference to the argument in question via parameter-expansion. echo1()( printf %s\\n ...


-1

A function, like an external command, receives a list of parameters. There's no way to define a function that intrinsically requires a certain number of parameters. The code of the function can check the number of parameters and their value and emit error messages if it wishes. The number of parameters passed to the current function is available in the ...


2

Strings in single quotes are used as-is. In a single-quoted string, the only special character is the ' single quote character, which ends the string. In strings in double quotes, the characters "\$are special:"ends the string,` makes the next character lose its special interpretation, ` starts a command substitution, and $ starts a variable substitution, ...


6

The character . is only excluded from wildcard matching when it's the first character of the file name and it would be matched by a wildcard. In the pattern .*, the * matches strings beginning with ., so .* includes .. (as well as ., with * matching the empty string). This is a straightforward consequence of the pattern matching rules, annoying though it may ...


1

As roaima said in a comment, you should use double quotes for variables you want expanded (so, "$location"), as single quotes protect variables from being expanded.


0

Yes, bash will create a temporary file with here-documents and with here-strings; you used the latter in your script. (You can test this by inspecting the /tmp directory while a test program with a sleep delay is running.)


1

I'd think you could use ls -A instead, specifically: chown -R username:groupname $(ls -A | grep '^\.') This does what you'd expect .* to do, match all files in the current directory that begin with a ., excluding . and ... But note this won't behave identically to a bash glob if you need it to match funky file names, like files with spaces in them.


1

I really dislike unexplained {} and \ markup and don't care much for the ; either! In the alternative, if the {} and \; are overly troublesome, there is an alternative approach. In addition, this approach handles spaces in the file name better than the find ... -exec formulation. find . -type d -print0 | xargs -0 chmod MASK find . -type f -print0 | ...


1

Summary: The shell performs parameter substitution on strings in double quotes but not on strings in single quotes. $$ is the shell PID but the number you see depends on which shell evaluates it. Details: Let us consider each case, one at a time. [root@localhost ~]# echo $$ 16991 16991 is the PID of the current shell: let's call it the main shell. ...


0

Single quotes are strong quoted, which bash will read as parameters, instead of a string as double quotes are intended for. Invoking bash with the -c option will tell it to use non-string parameters as positional parameters. Since single quotes are not strings, they must be interpreted as positional statements. ...


1

You can identify duplicate files using the following command: md5sum * | sort -k1 | uniq -w 32 -d


1

Not familiar with the "YAML front matter" but as per this page if it's present, your file will always start with --- so you could run: sed -n '1{/^---$/!q};1,/^---$/{/^Title: \|^Place: /d};p' infile i.e. if the first line doesn't match --- quit, otherwise delete all lines matching those patterns if they occur between the 1st line and the next line ...


2

I'm working on Linux, which means the is the command md5sum which outputs: > md5sum * d41d8cd98f00b204e9800998ecf8427e file_1 d41d8cd98f00b204e9800998ecf8427e file_10 d41d8cd98f00b204e9800998ecf8427e file_2 d41d8cd98f00b204e9800998ecf8427e file_3 d41d8cd98f00b204e9800998ecf8427e file_4 d41d8cd98f00b204e9800998ecf8427e file_5 ...


2

The colours are set by ls, using the LS_COLORS environment variable. To change the colours, you can use dircolors. dircolors --print-database outputs the current source settings, which you can store in a file and adapt; then dircolors ${file} will output the processed LS_COLORS value for you using the settings in ${file}. Strictly speaking ls outputs ...


1

A variation of Chris Down solution that filter just hidden directories and removes the -R options. Your original requirement was to change ownership and group classification of hidden directories, not their content. find /home/username -maxdepth 1 -type d -name '.*' -exec chown user:group {} +



Top 50 recent answers are included