Tag Info

Hot answers tagged

32

You could try adding the line: trap 'cowsay "Have a nice day!"; sleep 1' EXIT to your .bashrc file.


15

I found which alias or which compgen etc returns nothing. Both of those are shell builtins. which knows nothing of shell builtins: it simply searches a path for executables. For a more reliable result, use type. $ type compgen compgen is a shell builtin $ type alias alias is a shell builtin type has better knowledge of what executes because it is ...


8

The variable PATH is a special shell variable. It defines the list of directories in which executables/commands can be found. In the line PATH="${2}", you wipe out the default value of PATH and now the shell doesn't know where to find the ls command. For example, ls can be found in the directory /bin, but you modified PATH to have the value midhand.mp4 (the ...


8

It's sometimes useful to allow parameter expansions side-effects to occur. For example, setting a default value read -p "Enter your name: " name : ${name:=John Doe} # if the user entered an empty string echo "$name"


7

That looks fine to me. I would just make it a bit less "do or die": if "Y" then return 0 if "N" then return 1 That way you can do something like: if check_yes_no "Do important stuff? [Y/n] "; then # do the important stuff else # do something else fi # continue with the rest of your script With @muru's select suggestion, the function can be ...


7

Ah, there is something built-in: zenity is a graphical dialog program: if zenity --question --text="Is this OK?" --ok-label=Yes --cancel-label=No then # user clicked "Yes" else # user clicked "No" fi In addition to zenity, you can use one of: if dialog --yesno "Is this OK?" 0 0; then ... if whiptail --yesno "Is this OK?" 0 0; then ...


6

You can also use it for endless loops: while : ; do # .... done


4

You are getting this error message because you have changed the PATH environment variable to a command line argument (${2}) which dose not hold the actual binary.


4

Open Rhythmbox Music Player and import all your Music files. From the Rhythmbox menu select File -> Add Music -> Select a location containing music to add to your library (This should be set to your Music folder.) -> click the Import listed tracks button. Wait for a few minutes for Rhythmbox to import all the songs in your Music folder. How long it takes ...


4

It seems quite straightforward. romano@RRyS:~$ size=$(ls -l /var/spool | wc -c) romano@RRyS:~$ echo $size 476 The shell syntax $(command) executes command, and returns the standard output: just save it in a variable. Your command: ls -l /var/spool | wc -c > size will create a file named size in the current directory (containing the number and a ...


3

As per the question: (a) the script is in the same directory as blah.deb, and (2) the user will run the script from some unknown other directory. In that case: #!/bin/bash sudo dpkg -i --someflags=abcd "${BASH_SOURCE[0]%/*}/blah.deb" The shebang line is upgraded to bash so that we can use the BASH_SOURCE array which tells us the location of the script ...


2

This is not a problem, it is not dangerous and is entirely normal. You can get such lines if you hit space a few times and then hit enter. This will be saved in your history (since it is a non-blank line, spaces are characters just invisible ones). To test this, we can use a command that prints the blank lines in bash's history For example, this grep will ...


2

I don't really understand your issue (find should return the full path), however I suggest a different approach, perhaps something like find -name '*.report' -execdir bash -c 'echo mv "$0" "${0/LP_/LPP_}"' {} \; By passing the find result {} as a shell positional parameter $0 to a subordinate bash -c command, we can work around the fact that we can't do ...


2

You can use it to create a file without running a program:: : > /path/to/file This is infinitesimally faster than touch /path/to/file (since it doesn't require running the touch program) and may be marginally more portable than just plain > /path/to/file which seems to work on many systems.  Similarly, it can be used to check whether you have ...


2

You can use an infinite loop in bash: while true ; do # Your code here. sleep 30; done You can also schedule a periodic run of the checking program by cron.


2

Yes, you need an infinite loop with a sleep of 30 seconds. The following snippet will do: #!/bin/bash while true do # do any stuff you want echo "doing my thing" # sleep for 30 seconds sleep 30 done But I think that you will soon find that doing it in a bash script is probably not what you want to do. Tasks like this usually require some ...


2

You could do something like k=1; find source/ -type f | while read file; do [[ k++ -le 20 ]] && cp "$file" target/ done That will find all files in the current directory and copy the first 20 it finds to target/. The trick is the $k variable which is initialized to 1. Then, the [[ k++ -le 20 ]] && cp means "If $k plus one is less ...


2

Use a combination of find, sed and xargs: find /path/to/dir -print0 | sed -nz '1~2p' | xargs -0 cp --target-dir=/some/dir sed -n '1~2p' prints every alternate line, thus reducing the count to half. To make it a third, use 1~3p. The -print0, -z and -0 options indicate that we're using the \0 (NULL) character to delimit things. References: how to reduce ...


2

To do a fraction (e.g., ⅓) without knowing the count in advance, k=0; for file in * do if [ $((k++ % 3)) -eq 0 ] then mv "$file" target/ fi done This will grab every third file, by keeping a running counter (k) and acting on each file for which k is a multiple of 3.  Since I started the counter at 0, this will round up; e.g., if ...


2

Adding them to ~/.profile (apply to your user) or a /etc/profile.d/*.sh file (apply to the entire system) is the correct way. In order for the change to apply, you have to log out of your system and log back in, as ~/.profile and /etc/profile.d/*.sh are loaded when you login. To 'reload' ~/.profile in a running terminal, you can use source ~/.profile


2

The syntax highlighting that Stack Exchange offers is enough to see what's wrong. It's this line: alias perms='stat -c '%a - %n' The quote isn't closed. Try: alias perms='stat -c "%a - %n"'


1

After looking more clear-fully at the readline6 source package, more specifically at the readline-common.postinst file I discovered the file is copied at the configure step if [ "$1" = "configure" ] && [ "$2" = "" ]; then install_from_default /usr/share/readline/inputrc /etc/inputrc fi I really don't understand why it's copied this way and not ...


1

That usually means you have an error in ~/.inputrc or with a bind command in ~/.bashrc that accidentally binds m to something other than the self-insert readline function. Double check the two mentioned files.


1

As answered in the linked question, this is substring removal: ${string##substring} Deletes longest match of $substring from front of $string. In this case, it is removing the path and retaining only the basename: $ FILE=/etc/default/google-chrome $ echo ${FILE##*/} google-chrome $ basename $FILE google-chrome Ordinarily, I'd stick to basename, but ...


1

You don't need to use export; the PATH variable is already exported at that point. Anyway, .profile is read when you log in, so what you've done is correct, but opening a new terminal does not count as logging in.


1

To find files of a certain type, I wouldn't search for the file extension. I'd rather scan files for their MIME type using a combination of find and file. This output can then be greped for the desired mimetypesm e.g. audio/* and video/*. I've created a small script to do so: #!/usr/bin/env bash #Don't know whether you want to do so, but this deletes a ...


1

Definitely a more verbose option (python) that looks for mimetype(s) using the file command. It is an edited/rewritten version of this one, made fit for your purpose. What it does When files are found it returns a message (in the terminal): checking for filetypes: image, video, audio... 4 media files found. See for details: ...


1

You could create a .bashrc script or something like it, which is appended to the chroot env's /root/.bashrc, which does all the mounting etc. Aftwerwards you restore the backed up .bashrc in /root and exit the chroot: Main script: #!/usr/bin/env bash cp bashrcscript chroot/root/ if [ -a chroot/root/.bashrc ]; then cp chroot/root/.bashrc ...


1

The thing about chroots and /proc, /sys and /dev/pts is that these three filesystems are provided by the kernel, so they remain the same whether you mount within the chroot or from without. Indeed, you'll see, earlier on in the instructions: sudo mount --bind /dev chroot/dev /dev is populated by the kernel, but is not a kernel-provided filesystem, so it ...


1

Create a second script (e.g. chroot.sh) and place it in your chroot/ folder. Now edit the command in your original script to this: chroot chroot/ ./chroot.sh Now the script chroot.sh will be executed inside your chroot.



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