Tag Info

Hot answers tagged

18

The PID of the currently running script is $$ http://www.gnu.org/software/bash/manual/bashref.html#index-_0024_0024 ($$) Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.


8

To run your script at startup simply create a new cronjob. First open crontab by: crontab -e And then add the following line to it: @reboot /path/to/your/script


7

Assuming by path you meant path to a directory, use run-parts. From man run-parts: run-parts - run scripts or programs in a directory At first you need to set execute permission on all the scripts you want to run. Normally run-parts will ignore the directories and also the files that are not executable resides in that directory. Although before running ...


6

Solution to problem 1 and 2: You need to edit your /etc/sudoers (sudo visudo) file to add the relevant entry to grant the passwordless sudo permission for the user to run this script (not the shutdown command) : foo spamegg = (root) NOPASSWD: /path/to/script.sh Here foo is the username, spamegg is the hostname, replace the /path/to/script.sh ...


6

Use a shell glob instead of ls: select option in "Exit" "$1"/* . . . elif [ -n "$option" ]; then ls "$option" else . . .


5

In bash: #! /bin/bash for i do f="${i##*/}" y="${f:0:4}" m="${f:4:2}" d="${f:6:2}" x="${f:8}" mkdir -p "$d-$m-$y" && mv "$i" "$d-$m-$y/$x" done Save it as a script (say, mover.sh) and do: /path/to/mover.sh * Or: find /path/to/pics -type f -exec /path/to/mover.sh {} + Use a echo instead of mkdir and mv to test-drive ...


5

The main problem is that the $ is important to perl. When you use it in a system command, you need to escape it: $thirdlast=`df -H | awk '{print \$(NF-3)}'`; print $thirdlast; Otherwise, perl will try to expand $(NF-3) as a perl variable. The variable $( is the perl process's real GID: $( The real gid of this process. If you are on a machine that ...


5

Something like this should do: for file in ./*_???ILN*; do dir=${file%ILN*} dir=${dir##*_} mkdir -p "./$dir" && mv -iv "$file" "./$dir" done See FAQ 100 for more on string manipulations in bash.


4

You can make a conditional to relaunch the script as root if it's launched as a normal user. To shutdown the computer: #!/bin/bash if [[ $USER == "eka" ]]; then # If the script is ran as "eka": sudo $0 # Relaunch it as "root". exit 0 # Once it finishes, exit gracefully. elif [[ $USER != ...


4

Don't use setuid shell scripts, the SUID bit is not honored on shell scripts anyway on current systems. Use sudo instead, as you attempted anyway: eka ALL=NOPASSWD: /home/eka/test/test.sh And then: eka$ sudo ~/test/test.sh With that, test.sh will be executed as "root". No need to use setuid here.


3

You can use this modified script: #!/bin/bash # Script to add a user to Linux system if [ "$(id -u)" -eq 0 ]; then read -p "Enter username : " username read -p "Enter password : " password IFS=' ' read -a grps -p "Enter group names : " if grep "^${username}:" /etc/passwd &>/dev/null; then echo "$username exists!" ...


3

First install the package inotify-tools: sudo apt-get install inotify-tools A bash script would help #! /bin/bash folder=~/Desktop/abc cdate=$(date +"%Y-%m-%d-%H:%M") inotifywait -m -q -e create -r --format '%:e %w%f' $folder | while read file do mv ~/Desktop/abc/output.txt ~/Desktop/Old_abc/${cdate}-output.txt done What does this script ...


3

After some research I have managed to write a functional script. It will automatically disable touchpad while full screen application is active and mouse is connected. So it is suitable for full screen games. What you need to do is copy the text into Gedit and save file as e. g. : .auto_disable_touchpad.sh (Since it got . in front of name it is hidden file, ...


3

In the bash shell, you can remove a 'dot extension' (shortest trailing pattern matching a period followed by any number of characters) from a variable that contains a file name using parameter expansion e.g. if f=00004.time then ${f%.*} evaluates to 00004. Putting that in a loop over a shell glob, for f in *.time; do printf '%s\n' "${f%.*}"; done The ...


3

*/15 * * * * . /home/ashish/parser.sh cron uses sh, not bash, so when you source the script (that's what the . does), it is run under sh, not bash. Remove the .. Also, the PATH for cron is limited. Specify the full paths to commands you use, such as workon, or set PATH yourself.


3

Sounds like your script lacks a shebang line. Make sure the very first line of the script reads: #!/usr/bin/env bash or #!/bin/bash On a side note, you should avoid putting .sh extension on a bash script, since bash is not sh. Preferably use no extension at all.


3

First of all you need to comment out all the lines that are not scripts. #this is a comment sudo apt-get your command Also this will fail becaue you need a yes. sudo apt-get -y upgrade That will say yes for you. RISKY! You don't need sudo to work with xrandr. And you are never outputting your resolution. Here is an example of one of mine. xrandr ...


3

Run-parts will work if your scripts have the correct names. If you don't want to deal with renaming your scripts to fit run-parts's complex naming scheme, you could do something as simple as for file in ~/target/*; do $file 2>/dev/null; done That will attempt to execute all files (and directories) found in ~/target. The 2>/dev/null redirects error ...


3

Firstly you could replace the && with ; which is equivalent to the line breaks in your multiple-line code. Secondly I don't think you need the && (or ;) after the then statement. The difference between the && and ; line breaks is that && will run the next block of code if and only if the previous block has executed properly. ...


3

Global issues: Use indentation. It makes your code more readable (for you and for us). Use only straight quotes in bash. Other types of quotes might break commands. Do not append ; at the end of a single command. The ; character has a specific meaning and it's useless if appended to a single command. Specific issues: fdisk -l; You need to run fdisk as ...


2

If you cloned sources from Ubuntu git or using a deb source package, then debian directory is at the second level. Like that git clone git://kernel.ubuntu.com/ubuntu/ubuntu-precise.git cd ubuntu-precise And you will see "debian" in ls. But actually you do not need to set +x. It must be already there. Just do fakeroot debian/rules clean fakeroot ...


2

In bash, the latest backround process' PID is stored in $!: nohup <command> & pid=$! To send a process a signal you can use kill: kill -<signal> <pid> Putting the pieces toghether, to start e.g. a background watch -n1 echo foo process and send it e.g. a SIGTERM signal later: nohup watch -n1 echo foo & pid=$! # ... kill -15 ...


2

The script below will move and rename any file that might appear in a defined directory (dr1). It renames the files like: output_1.txt, output_2.txt` etc. The script looks "actively" if the targeted name already exists in directory 2 (not from a "blindly" chosen range), so you can start and stop the script at any time without the risk of overwriting existing ...


2

The script below will tile an arbitrary number of chrome or chromium windows in a Nx2 grid (N rows, 2 columns) where N depends on the number of open windows. If there is only one window, that window will be maximized (or unmaximized if it is already maximized). #!/usr/bin/env bash ################################################# # Exit if there are no ...


2

In addition to steeldriver's answer, as the substring to be excluded is same for all files, you can use substitution operation of the bash's parameter expansion: for file in *.time; do echo "${file/.time/}"; done | sort -n > file.txt This will replace .time with empty string i.e. .time is excluded from the file names. The syntax for substitution ...


2

Sounds like the script contains some CR (\r) characters. If you have edited this script from windows, that would explain why. Running this should reveal the otherwise "invisible" carriage return characters: sed -n l /home/pi/sh/test.sh (That's lowercase L, not 1). If you see a \r in the above, see FAQ 52 for various ways to get rid of them. On a side ...


2

The name of the package is gnome-terminal Here is a page with all of the switches you can use along with it http://manpages.ubuntu.com/manpages/karmic/man1/gnome-terminal.1.html


2

As far as i know, there are 5 Ways to install ubuntu: 1- Boot From a Live USB Drive or CD 2- Install Ubuntu On Windows With Wubi 3- Run Ubuntu In a Virtual Machine 4- Dual-Boot Ubuntu 5- Replace Windows With Ubuntu (Read More here)


2

This script controls the shutdown timer using sleep and a loop. #!/bin/bash # Read Password read -s -p "Password: " password echo # Send messages for i in {0..5} do echo "Shutdown in" $((30- i * 5)) "minutes" sleep 300 done echo "Shutdown now" | wall # shutdown and screensaver Command echo "$password" | sudo -kS shutdown -h +30 & ...


2

When you have two commands on two separate lines of a shell script, the second will only run when the first has finished. To avoid this, you need to add a & to the end of the first command to have it run in the background: #!/bin/bash sudo shutdown -h +30 & gnome-screensaver-command -a As for not asking for the password, you'll need to tell sudo ...



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