Tag Info

Hot answers tagged

18

In modern bash (version 4.2 and above): [[ -v name_of_var ]] From help test: -v VAR, True if the shell variable VAR is set


10

Use single quotes for the expression you used: sed 's/\//\\\//g' In double quotes, \ has a special meaning, so you have to backslash it: sed "s/\//\\\\\//g" But it's cleaner to change the delimiter: sed 's=/=\\/=g' sed "s=/=\\\/=g"


9

Creative and involving signals, you say? OK: trap on_exit EXIT trap on_usr1 USR1 on_exit() { history -a trap '' USR1 killall -u "$USER" -USR1 bash } on_usr1() { history -n } Chuck that in .bashrc and go. This uses signals to tell every bash process to check for new history entries when another one exits. This is pretty awful, but it ...


9

Your problem is not nested loops, per se. It's that one or more of your inner loops is running in a subshell. This works: #!/bin/bash for i in $(seq 1 100); do echo i $i for j in $(seq 1 10) ; do echo j $j sleep 1 [[ $j = 3 ]] && { echo "I've had enough!" 1>&2; exit 1; } ...


7

According to man bash: When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands ...


6

Ed is the standard editor, because you can use it to develop ed scripts and let it do its work, as you tested before, interactively. For small files like .bashrc or any code text, ed is performant because it reads the whole file in one rush and works with the buffer. For the same reason you should not use ed for big files like log files. But with an ed ...


6

These are not part of bash; find is a standalone program and does not require bash or even a POSIX shell to run. For example, it works fine with fish, which is not POSIX compliant and does not follow all the same syntax rules as bash. You could, in fact, use it with no shell at all (e.g., in a programmatic context). This is why (if you are using a POSIX ...


5

Try typing "set +x" at your shell. This should unset the -x option.


5

Using GNU Parallel it looks like this: parallel script1.sh {}';' script2.sh {} ::: a b c ::: d e f It will spawn one job per CPU. GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to. It can often replace a for loop. If you have 32 different jobs you want to ...


5

Check if the file's contents remain the same after sorting numerically and filtering duplicated lines: cmp file <(sort -n file | uniq) At least GNU sort can do this check directly: sort -c -u -n file (The POSIX sort documentation mentions this too so it should be supported everywhere.)


5

1) Do I just type cp .bash_profile .bash_profile.ORIG into the terminal right after I open it? Yes. You are essentially making a backup copy of your current ~/.bash_profile (assuming there is one). 2) How do I load it into my text editor? It depends on what text editor you intend to use. I do this: $ emacs ~/.bash_profile but you could also ...


4

Don’t believe everything a computer tells you. $ ps -p $$ PID TTY TIME CMD 3744 pts/1 00:00:03 bash $ ps -t pts/1 PID TTY TIME CMD 3744 pts/1 00:00:03 bash 4285 pts/1 00:00:00 ps $ echo foo | while read x do ps -t pts/1 done PID TTY TIME CMD 3744 pts/1 00:00:03 bash 4287 pts/1 00:00:00 bash 4288 pts/1 ...


4

Okay so i did some research and it is interpretting the first number as the year, it would appear that the + and entirely superficial and completely ignored. Using your examples. $ date -d'1 February' Sun Feb 1 00:00:00 CET 2015 Works entirely as expected as no year is supplied. $ date -d '1 February + 1 week' Thu Feb 8 00:00:00 LMT 0001 Almost ...


4

The environment of cron is not the same as the environment of your random login shell. Most likely something in your environment is not what your firewall script needs it to be. Try checking /var/mail, or whatever your mail spool is. Cron defaults to mailing the output to the user whose crontab it executes something from, but if you haven't configured your ...


4

Although both curly braces {,} and semicolons ; do have special meanings in bash, in this case it is the find command itself that is interpreting them, not the shell. The -ok command of find uses the same syntax as its -exec command, so you will find a complete description in that section of its manual page (man find): -exec command ; Execute ...


3

printf '%s\n' {♠,♣,♢,♡}$'\t'{{2..10},J,K,Q,A} | shuf -n5 | gawk 'BEGIN{ split(",Twos,Threes,Fours,Fives,Sixes,Sevens,Eights,Nines,Tens",vt,","); vt["J"]="Jacks"; vt["Q"]="Queens"; vt["K"]="Kings"; vt["A"]="Aces"; } # values-text { c[$2]++; printf("%s %s", $1, $2(NR==5?"\n":"\t")) } END{ for(i in c){ if( c[i]==2 ){ print ...


3

Try: sed 's/\//\\\//g' or using another delimiter to prevent you from escaping slash: sed 's,/,\\/,g'


3

The only ones that bash looks at by default are in the user's home directory, yes. There is also typically a single source for them in Linux -- /etc/skel. The user's home directory does not need to be under /home, though. I see you've edited your question to ask where your .bash_login and .bash_profile files are. Based on the # prompt, I'm going to assume ...


3

while read -d '&' -r data do myarr[${#myarr[@]}]="$data" done < <(sed '1! s/fafafafa/\&&/' 25.6.2015_test.txt) will put all your data from file 25.6.2015_test.txt into array myarr separated by fafafafa altogether with it. sed is used to put delimeter character & (you can use any which is not expected in the text) before ...


3

Problem So many things wrong here #!/bin/bash myarr = ( has got a space between it meaning nothing is assigned if it even runs at all. cat 25.6.2015_test.txt | awk Awk can open its own files no need for cat -F 'fafafafa' '$1~/^[a-z0-9*]+$/ -F is the field separator not record, so all this is doing is removing the text fafafafa, it's still reading ...


3

You can try following awk: awk 'NR == FNR { x[$1] = $1+0; next; } { for (i in x) { if (x[i] > $1+0 && x[i] < $2+0) { print x[i], $3; } } }' file1 file2 Result should be: 4 Alpha 2 Alpha 7 Bravo


3

execcmd="convert -monitor ${filearray[@]} output.pdf" But executing that with $execcmd convert is throwing numerous errors at me Don't call $execcmd, as it has already lost the differentiation between filenames and space-separated parts of filenames. Instead, execute the command itself with quoted arguments: convert -monitor "${filearray[@]}" ...


3

Use for: for i in */; do (cd "$i" && sh *ld); done


3

After looking into /etc/profile. The best place to do this is in /etc/profile.d. Place a script in this directory that looks like this: # add ~/bin to path for all users. PATH="${HOME}/bin:${PATH}" export PATH When /etc/profile runs it calls for sh in /etc/profile.d/*.sh ; do [ -r "$sh" ] && . "$sh" done This will run the new script in ...


3

I see two options. One, he can use tail -f to see the log file as it's being written, or two, he can have the program start inside a screen (or similar) session to which he can (re-)attach later. If he doesn't know the location of the log file, he can use top, ps or a similar tool to find the process ID, then run lsof -p1234 where 1234 is the process id to ...


2

It appears to be interpreting the 2nd digit as the year and then assuming 1 week(s), so always add a year, as this year then I think you will get what you desire. eg. christian@fujiu1404:~$ date -u -d'1 feb this year -2 weeks' Sun Jan 18 00:00:00 UTC 2015 christian@fujiu1404:~$ date -u -d'1 feb this year +2 weeks' Sun Feb 15 00:00:00 UTC 2015 ...


2

In vi you need to enter the key sequence for each of the 2 special characters, not the ascii chars for their display (if you place the cursor on them you'll see the entire ascii sequence is highlighted): Ctrl+v, x, 9, 3 instead of <93> Ctrl+v, x, 9, 4 instead of <94> So the search cmd would look something like this: :%s /Ctrl+v, x, 9, ...


2

If all you want is to get the file's name and use it to get the right target directory, you can do: #!/bin/bash for i in /home/userA/folder/*.txt do ## Get the file name str1="${i##*/}" ## Get the target directory dir="/home/userA/folder2/${str1%.txt}/" done This is using the shell's native string manipulation features. ${var##pattern} ...


2

Running gsettings reset-recursively org.gnome.system.proxy fixed the problem. There are two ways to define proxy settings: Manually set the http_proxy environment variable in /etc/environment or your bash profile. Define it in gnome system settings. I had not set the environment variable manually which is why recursive grep didn't find anything. ...


2

If you mean, check that each line is a decimal integer that is one plus the decimal integer in the previous line, then: awk 'NR == 1 {n = $0; next} $0 != n+1 {status = 1; exit} {n = $0} END {exit status}' Replace the $0 != n+1 with $0 <= n to check for strict increasing by any value, not only one. In that case though, you'll probably ...



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