Tag Info

Hot answers tagged

24

As you stated in your question, the main difference is the environment. sudo su - vs. sudo -i In case of sudo su - it is a login shell, so /etc/profile, .profile and .bashrc are executed and you will find yourself in root's home directory with root's environment. sudo -i is nearly the same as sudo su - The -i (simulate initial login) option runs the shell ...


24

Since you use brace expansion anyway so make use of its feature fully: echo {375..3500..5} To print each number in separate line with optional text: printf "Number %s is generated.\n" {375..3500..5} Number 375 is generated. Number 380 is generated. Number 385 is generated. ... Edit As pointed out by @kojiro in the comment Mac OS uses bash 3 as the ...


18

Alternatively a traditional C-style for loop can be used: for ((i=375; i<=3500; i+=5)); do echo $i done This is perhaps less clear than using seq, but it doesn't spawn any subprocesses. Though since I'm familiar with C, I wouldn't have any difficulty understanding this, but YMMV.


15

Using SEQ(1) for i in $(seq 375 5 3500) do echo $i done Or, simply: seq 375 5 3500


11

Your for-loop snippet didn't work as you require for two reasons: (($i += 5)) - here the $i is expanded to the value of i. Thus the expansion will be something like ((375 += 5)), which doesn't make sense (attempting to assign a literal number to another literal number). This would normally be achieved with ((i += 5)) (no $ to expand the variable) The ...


9

With zsh or bash4, you can use brace expansion for that: ls -d GLDAS_NOAH025SUBP_3H.A2003{001..006}.{0000,0600,1200,1800}.001.2015210044609.pss.grb >/dev/null Notice the brackets: {001..006} means expand to 001, 002, ... 006 {0000,0600,1200,1800} to every one of the above add 0000, 0600, 1200 and 1800. >/dev/null is to avoid the standard output of ...


8

To answer your question directly: no, there is no good reason to do this. Also, sudo su produces two log entries when one would suffice. I've seen many people do this, and when I ask why they don't just run sudo -s, the answer is just that they don't know about the -s flag to sudo, and generally they switch after I point it out. However, to your list of ...


6

There's no POSIX equivalent. You can only perform a redirection with exec, not a fork. A pipe requires a fork, and the shell waits for the child to finish. One solution is to put all your code in a function. all_my_code () { … } { all_my_code | logger --priority user.notice --tag "$(basename "$0")"; } 2>&1 | logger --priority user.error --tag ...


5

While there is, of course, an app for that (seq 375 5 3500), there are various ways of doing this from the commandline. While the fastest and simplest will be just using seq, here are some other options: for i in {375..3500}; do [[ (($i % 5)) -eq 0 ]] && echo $i; done i=370; while [ $i -le 3500 ]; do printf "%s\n" $((i+=5)); done perl -le ...


5

You can watch any command so give this a try watch "ps aux | grep python"


5

The argument to -c must be a single word, so su administrador -c "$OPENP_DIR/ctlscript.sh start" For "restart", you should "stop" first, then "start"


4

Google Compute Engine allows you to provision a Linux VM in your chosen region which you can then ssh into with root privileges. It's cheap and you can get $300 worth of usage as a free trial.


4

POSIXly: i=370 while [ 3500 -gt "$i" ] do echo "$((i+=5))" done ...or... echo 'for(x=370;x<=3500;x+=5)x' |bc I dunno why you'd do it any other way. Excepting, of course... seq 375 5 3500 ...or with dc: echo '370[5+pd3500>p]splpx'|dc


4

You can use the DEBUG trap to do this. In this trap, $BASH_COMMAND contains the command last executed. trap 'echo "you tried to call the command [$BASH_COMMAND]"' DEBUG Note that, if you are executing commands as part of your prompt or $PROMPT_COMMAND, the trap will run on these as well. You can add checks to see if $BASH_COMMAND is the same as ...


4

POSIX command/process substitution _log()( x=0 while [ -e "${TMPDIR:=/tmp}/$$.$((x+=1))" ] do continue; done && mkfifo -- "$TMPDIR/$$.$x" && printf %s\\n "$TMPDIR/$$.$x" || exit exec >&- >/dev/null { rm -- "$TMPDIR/$$.$x" logger --priority user."$1" --tag "${0##*/}" } ...


4

A variation on @chaos solution (bash 4.0 or above or zsh 4.3.11 and above): for a in GL.....2003{001..365}.{00..18..6}00.001.2015210044609.pss.grb do [[ -f $a ]] || echo "$a" done or for a in {001..365}.{00..18..6} do [[ -f "GL.....2003${a}00.001.2015210044609.pss.grb" ]] || echo "$a" done to print only the missing day+hour


3

exec bash should replace the current shell process with (a new instance of) bash.


3

The original Bourne shell, csh or tcsh all do not support $() and require `` for command substitution. The Bourne shell is no longer used as the default shell (/bin/sh) on many UNIX based operating systems. However, theses shells are still supplied by the OS vendors as they are still used for historical reasons. Both AIX and HP-UX have /bin/sh as a POSIX ...


3

zsh do not read .zshrc in non-interactive shell, but zsh allow you to invoke an interactive shell to run a script: $ zsh -ic 'type f' f is a shell function or you can always source .zshrc manually: $ zsh -c '. ~/.zshrc; type f' f is a shell function


3

In your link, the variable used outside (linecount) is not defined as a "loop" variable. It's just modified (incremented) inside the body loop, but not in the "while" statement. This is because when the "read -r f1 f2..." part is called, it reset (prepare) the variables used (f1 ..f7), wait for an input line and assign the variables according to the input. ...


2

You need more quoting when you pass arguments to SSH. Try this: ssh localhost "df -k / | awk '{print \$3/1024/1024/1024}'" 0 0.00623375 Note the \ before $3.


2

The Solaris 10 and earlier /bin/sh does not support $(). It is still in use today because Solaris 10 is still in use, Sun did not want to replace it with a standard conforming version (up to and including Solaris 10) - and because most people consider /bin/sh as the default shell and don't know how to portability execute a script with a POSIX conforming ...


2

If you're stuck on Bash 3: echo {375..3500} | tr ' ' '\n' | sed -n 'p;n;n;n;n' and if you prefer awk: echo {375..3500} | tr ' ' '\n' | awk '!((NR-1)%5)' I didn't know about brace expansion -- that is seriously cool.


2

Most probably you have hidden files in the folder. The point is that glob * selects only files and folders that do not start with .. So, if they do they are not passed to du command. On the other hand from top directory you get size of the directory as a whole, including dot files. To match all files in given folder, including hidden ones try (with bash) ...


2

Referring to 3.4.2 Special Parameters from Bash Reference Manual. Special Parameters: $* ($*) Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and pathname ...


2

There are two common approaches for this: find and shell globbing. find has an -exec option which lets you specify an action to be performed on each of its results. It makes each of find's results available as {} and you can modify them as you wish. It's slightly more complicated when you want to give arguments (such as echo foo > file). For that, you ...


2

Given the following tree (empty abc.txt): . ├── zyz │   └── abc.txt ├── zyz-1 │   └── abc.txt └── zyz-2 └── abc.txt You can list all the abc files with: $ echo zyz*/abc.txt zyz-1/abc.txt zyz-2/abc.txt zyz/abc.txt And you can use tee -a to append some input stream to all of those files at the same time: $ echo 'New line data' | tee -a ...


2

I suspect that you're looking for set -o notify, available in ksh, bash, zsh, and even POSIX sh, which causes job completion notifications to be printed immediately, even if you're at typing a prompt or if some other job is in the foreground. (If that's not what you're looking for, please clarify your question, maybe with an example of how it would look ...


2

With standard tools chest, sed is a good one: sed -e 's/"/\\&/g' <sampleMetadata


2

Actually, thinking about it, you can first affect all possibles: source | tr '\n<' '<\n' | paste -sd\\n - -| sed -e'/^[0-9]\{1,\}>/!{$!H;1h;$!d'\ -e\} -e'x;y/\n</<\n/;s//<&/' \ -ew\ /dev/fd/1 | filter ... | sink That will break your in-stream by first unconditionally swapping all < for \n and afterward conditionally ...



Only top voted, non community-wiki answers of a minimum length are eligible