New answers tagged

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 ...


0

What's probably happening is that some network equipment somewhere between the client and the server terminates connections that have been idle for a certain time. Such network equipment (firewalls, NAT appliances, …) often kills idle connections after a while to save memory (it's a defense against denial of service, but it does tend to be an annoyance to ...


0

I've found this nice piece of Emacs lisp that uses diff to compare outputs of export command before and after sourcing, and then calls setenv function accordingly: https://gist.github.com/ffevotte/9345586


0

I had a similar problem, I seemed to only see strange client_input_channel_req: channel 0 rtype exit-status reply 0. User I was trying to ssh into didn't have a shell by default. I ran the following: chsh -s $(which sh) username And then I was able to ssh. Note: Running su username was returning exit-code 1 (was failing) and now it just works.


0

According to bash's documentation, name A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier. word A sequence of characters treated as a unit by the shell. Words may not include unquoted ...


1

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


1

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


0

I suggest reading Section 8.4.6 Using fork and execve to Run Programs on http://www.groupes.polymtl.ca/inf2610/documentation/ComputerSystemBook.pdf


0

Shell variables are stored in the memory of the running shell. Use any data structure that lets you easily look up an item given its name; a hash table is a good choice. The difference between shell variables and environment variables is that environment variables are placed in the environment of subprocesses. All environment variables are shell variables. ...


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 ...


0

First, we can confirm that it is possible for you to recover deleted files on Mac after using rm command, even it is not an easy job. Usually, when you delete files or folders in through OS X terminal using the rm -rf, the content of the files or folders are not permanently erased. The system only removes the link to the file making it unable to be found or ...


0

And if you want to do that using pure shell script and dont want to use find, try this little one: #!/bin/bash #Simple bash recursive loop search - Luciano A. Martini =) nextdir(){ cd "$1" &>/dev/null for f in *; do if [ -d "$f" ] && [ ! -L "$f" ]; then #echo "Inside folder: $PWD/$f" nextdir "$f" cd .. ...


2

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


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 ;;


0

the quotes are needed because of the way zip handles multiple arguments: rm: remove all files in the argument list zip: unzip the file in the first argument. only extract the files in the remaining arguments. $ ls *.zip file1.zip file2.zip file3.zip $ unzip *.zip Archive: file1.zip caution: filename not matched: file2.zip caution: filename not ...


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 ...


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 ...


0

There is a Zsh plugin created to do specifically what you request: https://github.com/psprint/ztrace


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 ...


20

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 ...


48

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 ...


1

Lets begin by saying that all I/O whether file based, interactive or any other way require unique file descriptors for each. The standardization of interactive file descriptors is what allows redirection and piping. The shell is an expert at standard I/O manipulations. In your example, cat is called with its STDIN set to the shell's output representing ...


0

If running nemo from a terminal works for you until you close the terminal, then run nohup nemo & With nohup, Nemo won't be killed when you exit the terminal.


2

It's a kind of necroposting but I've had the same problem recently (with a different backend) and found that the reason is in a wrong Content-Type. By default it's "text/plain" or "text/html", and in my case curl -H "Content-Type: application/json" -d ... solved the issue.


3

You can try adding the bg mount option for nfs, if you have it. man 5 nfs: If the bg option is specified, a timeout or failure causes the mount(8) command to fork a child which continues to attempt to mount the export. The parent immediately returns with a zero exit code. This is known as a "background" mount.


0

Are you really using a shell script and not fstab to mount your drives at boot ? Read up on /etc/fstab (run the command man fstab) and in particular, look at the _netdev option, so you end up with something like. 10.10.10.10:/srv/share /mnt/mymnt nfs4 defaults,noexec,nosuid,_netdev,auto 0 0 Disclaimer: Do not copy paste the above without ...


2

Here's a bash-centric solution: IFS=, read -r -a vars <<<"$line" printf "%s\n" "${vars[0]}=${vars[1]},${vars[2]}" "${vars[0]}1=${vars[1]}" "${vars[0]}2=${vars[2]}" The first line splits your $line variable into pieces based on the commas. The second line, working from the inside-out: outputs the first value (e.g. 'dog'), equals-sign, then the ...


0

You could do something like this. I define the line as you have. Next, I parse our the first comma-separated token from the line (I assume here that will be valid for a variable name). Then I construct two new variables based on the first token, and assign them to the second and third comma-separated tokens from the line. line="dog,/path1,/path2" ...


1

If you are using bash, you can do something like this (with extra echo statements to show what's happening at each stage): $ line=dog,/path1,/path2 $ newline=$(printf "%s\n" "$line" | perl -n -e 'BEGIN {$count=1}; my ($var,@values) = split /,/; print "$var=" . join(",",@values); ...


2

I think that's the thing you're trying to do: line='dog,/path1,/path2' IFS=', ' read -r -a dog <<< "$line" echo "$dog" dog echo "${dog[1]}" /path1 echo "${dog[2]}" /path2 This assumes that your shell is bash.


3

Something like this: vimdiff <(find /home/masi -printf "%P %u:%g %m\n" | sort) <(find /home/masi_backup -printf "%P %u:%g %m\n" | sort) (this gives names without the leading /home/masi or /home/masi_backup, owning user and group, and permissions — the latter weren't mentioned in the question but seem useful, drop %m if you don't want them).


1

readlink -f "$(type -P sleep)" or if you're performance-conscious: cpath="$(type -P sleep)"; [ ! -L "$cpath" ] || cpath="$(readlink -f "$cpath")" Using readlink -e (existing) instead of readlink -f can save you from this kind of accident where you operate on a nonexisting file. The second example assumes the path returned by type -P is canonical, which ...


2

according to your comment " I have a text file where where all the names are listed without the extension. I want to make a PHP script that compares the text file with the folder to see which file is missing " : for file in $(cat yourlist) ; do [ -f "${file}.png" ] || { echo "$file : listed in yourlist, but missing in the directory" } done #assumes ...


1

If you have the GNU version of more installed: more -d -f -10 foo This displays the file foo 10 lines at a time, pausing with a prompt messsage after each 10 lines. Press spacebar or Enter at each pause to display the next 10 lines. You can also press h or ? at the more prompt to see more's other capabilities. see man more for more details. BTW, if ...


0

mv 'old.file(1).gz' new.file.gz should fix the problem.


0

Try this instead: find . -type d -exec sh -c "cd {} ; ls | grep -v '^filesNames.txt$' > filesNames.txt" \; This finds all directories beneath the current directory and, for each one, it cds into the directory and writes the directory listing to a filesNames.txt (excluding that file from the list). If you want the filesNames.txt files to be in the ...


1

A similar response to that of @steeldriver #!/bin/bash printf "Input text:" && read UI ; read -a UIS <<< ${UI} ; X=0 ; for iX in ${UIS[*]} ; do printf "position: ${X}==${iX}\n" ; ((++X)) ; done ;


6

If you are using bash, its read command has a -a option for that. From help read Options: -a array assign the words read to sequential indices of the array variable ARRAY, starting at zero So $ read -a s This is a sentence. Note that the resulting array is zero indexed, so $ echo "${s[0]}" This $ echo "${s[1]}" is $ echo "${s[2]}" a $ ...


0

It may be that the following is better suited to what you are trying to accomplish: # #// FILE could be a for-loop as well for example. FILE="bash_scropt.sh" ; if grep '#!/bin/bash' $FILE 1>/dev/null ; then printf "$FILE bash-script\n" ; else printf "> $FILE -- NOT bash\n" ; fi ; You can also mix this with @netmonk suggestion where a grep on ...


0

You can also use the standard ̀file command : [PRD][]user@localhost:~ 17:21:30 $ head -n 1 setproxymkt.sh #!/bin/bash [PRD][]user@localhost:~ 17:21:38 $ file setproxymkt.sh setproxymkt.sh: Bourne-Again shell script, ASCII text executable


1

Per your comments, you only have to descend one level deep. In that case you could use a glob to iterate over directories names and for each dir save file paths in an array, then print the last element of each path to fileNames.txt: #!/bin/sh for dir in "$PWD"/*/; do arr=( "$dir"* ) printf %s\\n "${arr[@]##*/}" > "$dir"filesNames.txt done To exclude ...


1

You need to cd out of each subdirectory before trying the next one. Inserting... cd .. at the end of the loop would fix this for the subdirectories, but breaks when you do it in the top directory first. A cd .. from there will take you up another level, so the other directories aren't visible any more. You can address all of this by saving the top ...


0

Simply adding my sudo /Morreels/./launcher line to the .bashrc file in /home/pi folder ran everything and fixed it !


0

#!/bin/bash for X in $PWD/* ; do if [ -d "$X" ] ; then # first depth directories cd "$X" ; files="$(ls)" ; printf "$files\n" >> filesNames.txt ; else # regular files: printf "$X\n" >> fileNames.txt ; fi ; done ; It may be that what you are trying to achieve ...


3

jobs shows the jobs managed by the current shell. Your script runs inside its own shell, so jobs there will only show jobs managed by the script's shell... To see this in action, run #!/bin/bash sleep 60 & jobs -l To see information about "jobs" started before a shell script, inside the script, you need to treat them like regular processes, and use ...


0

This does what you ask for: [ "${OldIP:-"$StartIP"}" != "$StartIP" ] && echo "OK" As also this (more complex): ${OldIP:+false} || { [[ $OldIP != $StartIP ]] && echo "OK"; }


1

A simple shell line (ksh, bash or zsh; not dash): set -- *.png; printf '%s\n' "${@%.png}" A simple function (from No Extension): ne(){ set -- *.png; printf '%s\n' "${@%.png}"; } Or a function that remove any extension given (png by default): ne(){ ext=${1:-png}; set -- *."$ext"; printf '%s\n' "${@%.${ext}}"; } Use as: ne jpg If the output is an ...



Top 50 recent answers are included