Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.).

learn more… | top users | synonyms (2)

6
votes
3answers
3k views

exit shell script from a subshell

Consider this snippet: stop () { echo "${1}" 1>&2 exit 1 } func () { if false; then echo "foo" else stop "something went wrong" fi } Normally when func ...
6
votes
3answers
364 views

How you stop 'wget' after it gets a 404?

If you use brace expansion with wget, you can fetch sequentially-numbered images with ease: $ wget 'http://www.iqandreas.com/sample-images/100-100-color/'{90..110}'.jpg' It fetches the first 10 ...
6
votes
2answers
695 views

Detect if running in a virtual terminal?

I'd like to autologin to tty1 on login and then use vlock to lock it. How can I detect from Bash if the current terminal is a console virtual terminal (e.g. tty1), so that I can put the vlock line ...
6
votes
3answers
108 views

generate links of pages with incrementing variable

consider the links http://unix.stackexchange.com/questions/tagged/linux?page=2&sort=newest&pagesize=15 ...
6
votes
1answer
271 views

How to find out whether a shell script is written in bash or sh

How can I find out whether a script is written in bash or sh? The first line of the script is not helpful here, since on Linux, bash scripts have this line: #!bin/sh Actually, there are many ...
6
votes
2answers
208 views

Decode Flags for Kill Command

I am new to Shell Scripting. I saw this Command on a Book for Linux shell Scripting $ kill -HUP 1234 >killout.txt 2>killerr.txt I know what the command does, but i am not able to understand ...
6
votes
6answers
9k views

Show sum of file sizes in directory listing

The Windows dir directory listing command has a line at the end showing the total amount of space taken up by the files listed. For example, dir *.exe shows all the .exe files in the current ...
6
votes
3answers
3k views

Is there a way to call a command with a set time limit and kill it when that time passes? [duplicate]

Possible Duplicate: Run a command for a specified time and then abort if time exceeds I was working on a continuos integration build script when a need for such a command arose. Basically ...
6
votes
3answers
156 views

Attempted assignment to non-variable?

We can use arithmetic operations inside shell script function: function mess { if (( "$1" > 0 )) ; then total=$1 else total=100 fi tail -$total /var/adm/messages | more } I ...
6
votes
5answers
10k views

Add lines to the beginning and end of the huge file

I have the scenario where lines to be added on begining and end of the huge files. I have tried as shown below. for the first line: sed -i '1i\'"$FirstLine" $Filename for the last line: sed -i ...
6
votes
3answers
1k views

How to tweet using terminal?

I would like to tweet a message using terminal. I tried something like: curl -u 'TwitterUsername':'TwitterPassword' -d status=”Your Message Here” https://twitter.com/statuses/update.xml but seems ...
6
votes
4answers
6k views

Why is my variable being localized in one 'while read' loop, but not in another seemingly similar loop [duplicate]

Why do I get different values for $x from the snippets below? #!/bin/bash x=1 echo fred>junk ; while read var ; do x=55 ; done <junk echo x=$x # x=55 .. I'd expect this result x=1 cat ...
6
votes
2answers
6k views

Can I make scripts use aliases instead of commands?

I have an alias for a command (I'm setting up a Python development environment) alias python=~/virtualenv/bin/python so that I can run ~/virtualenv/bin/python by just typing python. Now in my ...
6
votes
3answers
202 views

What is the difference between invoking a perl script via sh or perl

I have a simple Hello World perl script. #!/usr/bin/perl print 'Hello world.'; It works fine if I run it via perl <file_name>, but it fails I run it via sh <file_name>. My ...
6
votes
3answers
725 views

How enable safeguards for CPU temperature

My desktop has a nasty habit. When I have several high intensity applications running and my CPU is at maximum usage for a period of time, the core temperature rises and my computer auto-shuts off. ...
6
votes
1answer
1k views

Reason for ksh obsoleting -eq

The latest version of ksh obsoletes using -eq within [[ ]] blocks, prefering (( )) instead. Why is this? I can't find any documentation on the advantages of (( )) over [[ ]] anywhere, and I find that ...
6
votes
2answers
163 views

Is there a way to convert a zip to a tar without extracting it to the filesystem?

Is there a way to convert a zip archive to a tar archive without extracting to a temporary directory first? (and without writing my own implementation of tar or unzip)
6
votes
2answers
5k views

Good method to compare time?

I need to check for the current time and abort the script if it is not proper time of day I am supposed to run it. Also, if anyone else runs it, it should abort. For example: I need my script to only ...
6
votes
4answers
1k views

Why do I need to hit enter to get my shell prompt after my init.d script completes?

I had to write my own CentOS init.d script for celery because it only ships with one for Debian. You can see the script I wrote when I answered my own stack overflow question 3989656. But there's a ...
6
votes
3answers
74 views

Print a multiline variable in a column

I have a script that collects values from a cluster. The values, in some cases, have multiple lines. I have a printf format that specifies how the data should be positioned however, it doesn't take ...
6
votes
3answers
1k views

reliable return code of background process

Let's assume the following piece of bash code: foo > logfile 2>&1 & foo_pid=$! while ps -p$foo_pid do ping -c 1 localhost done wait $foo_pid if [[ $? == 0 ]] then echo "foo ...
6
votes
2answers
605 views

How does this find command using “find … -exec sh -c '…' sh {} +” work?

@StephaneChazelas posted the following solution to this Q&A: Having some trouble using “find -exec {} +”. $ find . -iname "*.extension" -exec sh -c ' exec <command> "$@" <additional ...
6
votes
3answers
10k views

How to set global environment variables at boot through a script, and have them available for an application that runs before login?

I have a service that runs at boot, and in that service it calls a bash script in the background that exports some environment variables. The problem I'm having is that those environment variables ...
6
votes
2answers
82 views

Passing arguments from a file to a bash script

I' ve got this situation: ./ ./myscript.sh ./arguments.txt ./test.sh Inside myscript.sh, i have to run the file test.sh, passing to it the arguments contained inside arguments.txt. myscript.sh is: ...
6
votes
1answer
60 views

BASH return to main function

I have a BASH script that calls a function, which calls other functions: #!/bin/bash function foo { function bar { # do something } bar } foo How can I return from bar ...
6
votes
1answer
87 views

Automatic logging of scripts executed by users

That's a recurrent question of my workmates. We are a team of many people which works in the same Unix and Linux servers. The recurrent questions is if there are a log or something similar by default ...
6
votes
1answer
655 views

what is the zsh equivalent of bash's export -f

So I started using zsh. I like it all right. It seems very cool and slick, and the fact that the current working directory and actual command line are on different lines is nice, but at the same time, ...
6
votes
2answers
272 views

Why does set -e not work inside () ||

I've run across some scripting like this recently: ( set -e ; do-stuff; do-more-stuff; ) || echo failed This looks fine to me, but it does not work! The set -e does not apply, when you add the ||. ...
6
votes
1answer
134 views

Determine dynamically allocated port for OpenSSH RemoteForward

Question (TL;DR) When assigning ports dynamically for remote forwarding (a.k.a. -R option), how can a script on the remote machine (for instance sourced from .bashrc) determine which ports were ...
6
votes
2answers
260 views

Cache all gpg subkey passwords at once? Prevent need for multiple gpg password entry?

Can I enter my gpg password just once and unlock all my sub keys (signing, decryption, authentication)? At the moment, I need to enter my gpg password three times (for signing, for decryption, for ...
6
votes
1answer
128 views

Where can I find out how to write a shell script that is as portable as possible? [duplicate]

Possible Duplicate: Resources for portable shell programming I want to write a shell script that is intended for further distribution. So I'd like to write it in a way that is as portable ...
5
votes
4answers
5k views

Is there a way to sum up the size of files listed?

This is the command I am using to list some files: find . -name \*.extract.sys -size +1000000c -exec ls -lrt {} \; -rw-r--r-- 1 qa1wrk15 test 1265190 Sep 29 01:14 ...
5
votes
5answers
5k views

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
5
votes
4answers
8k views

Processing bash variable with sed

Been banging my head off a wall on this bash variable LATLNG contains a latitude & longitude value in brackets like so (53.3096,-6.28396) I want to parse these into a variable called LAT and ...
5
votes
3answers
2k views

Prompt user to login as root when running a shell script

The problem I am getting is, when I enter the command, su - root at the beginning of my shell script file, it prompts the user to enter the password and then does NOT continue with the rest of the ...
5
votes
4answers
402 views

Shell, Concatenating 2 strings to reference a 3rd variable

I am trying to do the following in my script: #/bin/bash -x var1=www var2=www2 var3=www3 var4=www4 for i in 1 2 3 4; do echo \$var$i done Obviously, this doesn't work as expected and results in ...
5
votes
3answers
6k views

Storing output of command in shell variable

I have an operation using cut that I would like to assign result to a variable var4=echo ztemp.xml |cut -f1 -d '.' I get the error: ztemp.xml is not a command The value of var4 never gets ...
5
votes
3answers
893 views

Is there a more elegant way to count words and assign that count to variables?

I have a script: #!/bin/bash /root/xiotech status > xiostatus.tmp SyncCount=$(grep -c Sync xiostatus.tmp) PauseCount=$(grep -c paused xiostatus.tmp) CopyingCount=$(grep -c Copying xiostatus.tmp) ...
5
votes
4answers
425 views

How can I “lazily” read output from xrandr?

I have a bash script that I use to adjust my monitor brightness that uses xrandr --verbose to get the current brightness. It works fine, but using xrandr is kind of slow on my machine, as you can see ...
5
votes
5answers
443 views

How to concatenate two files on the fly and reference result as new file?

I want to concatenate two files, i.e. staticEntries.dic and dynamicEntries.dic. As the names might show, the content of staticEntries.dic stays the same over time (contains common words), but to the ...
5
votes
4answers
3k views

Building paths robustly

Say I have several variables in a shell script (e.g. in zsh): FOLDER_1, FOLDER_2, etc. These variables refer to folders descending from /. For example, if I have a path /home/me/stuff/items the ...
5
votes
3answers
1k views

Use & (ampersand) in single line bash loop

I have been using this command successfully, which changes a variable in a config file then executes a Python script within a loop: for((i=114;i<=255;i+=1)); do echo $i > numbers.txt;python ...
5
votes
3answers
577 views

Walking through files in directory randomly

How to change for track in *.mp3; do so that all .mp3 files in the current directory are walked through but in a random way?
5
votes
4answers
584 views

generate consistent machine unique ID

Can we generate a unique id for each PC, something like uuuidgen, but it will never change unless there are hardware changes? I was thinking about merging CPUID and MACADDR and hash them to generate ...
5
votes
2answers
278 views

Why doesn't my loop over the output of ls work?

I am trying to feed Directory names into a for loop. My code is as follows: td='/Test/TopDir/' cd "$td" for x in $(ls -d */ | cut -f1 -d'/'); do echo x=$x done The top directory I run this on ...
5
votes
5answers
2k views

How to parse ISO8601 dates with linux date command

I'm trying to use the date command to generate a file timestamp that the date command itself can interpret. However, the date command does not seem to like its own output, and I am not sure how to ...
5
votes
4answers
440 views

sed how to substitute when string has “http://” in it?

I have a file xx that has the following contents: @base_url = "http://dmstaffing-stage.herokuapp.com/" I want to use sed to eliminate this line (replace with nothing). I have used this sed ...
5
votes
4answers
1k views

how do I trim each line in bash?

I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into?
5
votes
3answers
191 views

${#array} vs ${#array[@]}

As far as I can tell both ${#array[@]} and ${#array} evaluate to the number of elements in $array. Is there any reason for preferring the longer form (${#array[@]})?
5
votes
4answers
4k views

Use a variable reference “inside” another variable

I'm sure it is relatively simple, I just don't know how to do it. #!/usr/bin/ksh set `iostat` myvar=6 I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value ...