Hot answers tagged

53

You've explained the situation very well. The final piece to the puzzle is that unzip can handle wildcards itself: http://www.info-zip.org/mans/unzip.html ARGUMENTS file[.zip] ... Wildcard expressions are similar to those supported in commonly used Unix shells (sh, ksh, csh) and may contain: * matches a sequence of 0 or more ...


21

The difference between those two commands is the quoted * character. If you call a command in a shell and use the * character for an argument, the shell itself will evaluate the argument. See this example: $ ls file1.zip file2.zip file3.zip file4.txt Now with a *: $ ls *.zip file1.zip file2.zip file3.zip The shell evaluates the wildcard and builds ...


6

The difference is in the first case the shell itself expands the glob: % cd / % echo * Applications Library Network System Users Volumes bin cores ... % while in the second case the application itself Does Something™ with that literal character: % cd / % perl -E 'chdir "/tmp" or die; say for ...


4

That's not the here string, it's ANSI-C quoting: Words of the form $'string' are treated specially. ... The expanded result is single-quoted, as if the dollar sign had not been present. So what you've got is a single-quoted string to the right of <<<. That string gets taken as the here string, with no further processing. There's no need to ...


3

There is a BIG difference. Lets take the following script called testscript: #!/bin/ksh #im testscript cd /proc/$$ file exe First lets execute it with ./: $ ./testscript exe: symbolic link to /bin/ksh93 Now calling bash: $ bash testscript exe: symbolic link to /bin/bash Do you see the difference? The interpreter used by the script changed in the ...


3

First, do not parse ls. There are many reliable ways of getting file names but ls is not one of them. The following uses the shell's globbing to generate file names and passes a nul-separated list of them to xargs which runs them through cat: printf '%s\0' * | xargs -0 cat I understand that the point of this was to demonstrate files->xargs. ...


2

You could use the pathname of your external command "func", e.g. /usr/bin/func.


2

The official way to prevent function definitions from being used by the shell is to call: command func See: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html


2

Your fonts and colors would be determined by the type of terminal emulator and where it is run. Some people do something like ssh -X remotehost -e xterm and some do this ssh (running in a terminal on the local host). Technically the local X display determines the maximum number of colors which you can use (in either case), but most users have ...


2

Single find command will output all the files with absolute path find $(pwd) -type f


2

I would do this: names=$(sed s/^/Name=/ file1.txt) ed file2.txt <<END /^end$/i $names . wq END now: $ cat file2.txt <project> <target> start Name=Ramesh Name=Suresh Name=Raman end </target> </project>


2

The shell being run when you log in to read your commands and run them keeps all the variables. If you run a 2nd shell, then it will have its own collection of variables. You can run the set command to see a list of the variables. The set command runs inside the shell instead of launching a new process. Environment variables are either kept in a separate ...


2

Use chown's recursive option: chown -R owner:group * .* Specifying both * and .* will match all the files and directories that find would. The recommended separator nowadays is : instead of .. If you want to change the current directory's ownership too, this can be simplified to chown -R owner:group .


2

less $(find . -name myfile.txt) less `find . -name myfile.txt` The first is, I believe, both POSIX-compliant and nest-able. The second, I believe, is more portable.


2

Similar to @coffeeMug, this is the more up-to-date way to doing this as it is apparently faster: find . -name "*.log" -exec ls -l '{}' + I'll also point you to CommandLineFu, which is always helpful with these things.


2

A command will receive the arguments after they have been processed by the shell. On first processing, an unquoted * will be expanded by the shell (to the list of files in the present directory (pwd) that match the pattern): echo *.zip Will list all .zip files. But echo "*".zip" will not. On first processing, a quoted "*" will not be expanded, it will ...


1

#! /bin/bash ( sleep 4 ) & # <-- The long running process. seconds=1 while jobs %1 &>/dev/null ; do echo $((seconds++)) seconds complete sleep 1 done echo Done. jobs %1 fails once the job %1 has stopped. Note that for longer times, $seconds might get out of sync with the real time. It's better to store the start time and ...


1

You could use this script: #!/bin/bash TEMPFILE="$(mktemp)" STARTTIME="$(date +%s)" (./longprocess; rm -f "$TEMPFILE") & while [ -f "$TEMPFILE" ]; do sleep 1s NOW="$(date +%s)" if (( (NOW-STARTTIME) % 300 == 0 )); then echo "$(( (NOW-STARTTIME)/60 )) minute(s) elapsed" fi done echo "Done!!!" It executes your longprocess in a ...


1

You can achieve this using find's -exec flag: find . -name "*.log" -exec ls -l '{}' \; In this example find searches for all log files in current directory and then list them using ls -l. In your case you should replace ls with less. See the ACTION part of find man page here find(1) man page.


1

This uses sponge from Debian's moreutils package. No bashisms. Backup the four input files however you like, (since this will change them), then: # enumerate headers of each file, then sort each file, in place for f in file* ; do sed '1s/.*/&'"$f"'/;s/file//' $f | sort | sponge $f done # join sorted files, output to 'fileN' for f in 34 N2 N1 ; do ...


1

man 5 crontab suggests that a cronjob will find the owning user's username in the LOGNAME environment variable: Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab´s owner. HOME and SHELL can be overridden by settings ...


1

Get the script's owner On any system with as stat that is compatible with modern GNU stat, the user ID of the owner of the script is: stat -c %u "$0" The user name of the owner of the script is: stat -c %U "$0" In general on linux, stat -c %U file returns the owner of file. We substitute in $0 because that variable typically contains the name of ...


1

You could use awk awk 'NR==FNR{Lines=Lines "Name=" $0 "\n";next}/end/{print Lines $0 ;next}1' file{1,2} <project> <target> start Name=Ramesh Name=Suresh Name=Raman end </target> </project>


1

It's only a variable for the duration of the program's execution. Another way of thinking about this is by rewriting your program to the following. #!/bin/bash b=1 echo $b exit Now, imagine you're doing that within a terminal, then exiting that terminal session. Opening another terminal session won't show you anything regarding that b=1 assignment in ...


1

I would put environment variables in .bash_login or .bash_profile, since they are (when exported) inherited to subshells and don't need to be reset for every shell invocation. Not that resetting them would cost practically anything, but in case I want to set an envvar to something else for the duration of a subshell. That's hard to do if the .bashrc ...


1

Since the question is essentially asking for opinions, you will get different answers. A good design practice, for example, uses as few places as possible to provide a given feature. Some put information like this in the system area to reduce the amount of work done by users to customize their shell environment. In my environment, I use the terminal ...


1

I put PS1 code in bashrc all the time. My code is as follows: export PS1="\[\e[01;37m\][\[\e[0m\]\[\e[01;32m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;34m\]\h\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;37m\]\t\[\e[0m\]\[\e[01;37m\] \W \e[1;37m(\e[1;32m|\e[1;33m|\e[1;31m|\e[1;37m]\\$ \[\e[0m\]" I use a different PS1 for root (/root/.bashrc) - red username, ...


1

Using a backslash or other forms of quoting on the name of a function has no effect. In the shell's syntax analysis, functions are looked up long after quotes have been parsed. You or the person who wrote what you read is confusing functions and aliases. Aliases are parsed much earlier, and quoting any part of a command name makes it ineligible for alias ...


1

Your case statement could look like this S*) echo Starts with S if [[ -f x && -f x.csv ]] then echo File x and x.csv exist else echo input file missing fi ;;


1

I know this is an old question but i have been searching all night for a similar solution. I found a few helpful tips but they did not do exactly what i needed, so I had to mix and match a few to get the correct outcome I was looking for to simply remove special characters and replace them with a (.) dot for f in *.txt; do mv "$f" `echo $f | sed ...



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