Tagged Questions

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

learn more… | top users | synonyms (2)

4
votes
1answer
388 views

Writing command synopsis in standard man format

I am writing a script and in the usage() function I want to specify the usage. My script uses either option1 or option2 or both. One of them is mandatory. Is there a standard way to write it up?
1
vote
2answers
947 views

To pass a parameter with sed

For my script I have to read the data from a file. For example I have 2 files: one is a text file, the second is a csv file with 10 columns. What I am trying to do is to read the csv and change the ...
11
votes
4answers
6k views

How to define and load your own shell function in zsh

I am having a hard time defining and running my own shell functions in zsh. I followed the instructions on the official documentation and tried with easy example first, but I failed to get it work. ...
1
vote
2answers
835 views

How to change user with authentication inside shell script

It is possible to change the user within a shell script by su - newuser; but what if the newuser needs authentication. How can I provide the password within the shell script? None of the users is ...
0
votes
2answers
724 views

Automated SSH Login [duplicate]

Possible Duplicate: Shell Script for logging into a ssh server Autentification on SSH connection in OneLine How could I make a SSH Login programmatically on a unix server, I don't have ...
5
votes
2answers
283 views

Is it safe to remove a script file from within that script?

Imagine I have a script foo. It should be run once when the user logs in and isn't needed after a successful run. My question: Is it safe to remove the script file from within the script? E.g.: ...
3
votes
1answer
330 views

Shell script doesn't paint last line of stdout to screen without user input

I have a shell script that's happily doing all my backup process and writing progress to stderr and stdout, with one exception — the last line doesn't get written to my terminal (if I call the script ...
2
votes
3answers
3k views

Faster awk script to get the substring / string we wanted

ORDER EVENT .........[] [] ... so many other tags... [Account<25106>=ACCT1] [Destination...] .. so many other tags. I am currently trying to get the account like this. I tried using match in ...
24
votes
3answers
49k views

What characters do I need to escape when using sed in a sh script?

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
4
votes
1answer
697 views

Best way to swap filenames

I need to swap filenames of two files (file and file_1). I'm using the following code for it. mv file .phfile mv file_1 file mv .phfile file This works but is very buggy, It sometimes even results ...
4
votes
5answers
1k views

Execute string result from shell script as a set of shell commands

I create a string result from my shell script and i want to execute it as a shell command. For example file_a is: user1@gmail.com user2@gmail.com script: awk '{print "mail -s \"welcome\"", $1}' ...
0
votes
1answer
431 views

Shell script to check server state

I am writing a script that would ssh into the VMWare server and check if the VMs are up or not. There's some error in the server=awk "/$i/{print $2}" /home/dharmit/scripts/vmids line. I am getting ...
1
vote
1answer
243 views

Background script run at login interrupted when I run tmux in terminal

In ~/.profile I run a script in the background (redshift for changing the screen temperature at night time). After logging in, I can open a terminal, and run tmux. However, at this point, redshift ...
1
vote
2answers
972 views

The receiving of returns by Nagios from script through NRPE

I am using nagios3 and want to collect some information from nrpe_server. Next script is at nrpe_server: #!/bin/sh #set -x # script for checking disk usage on ZFS # requires min zpool version 13 or ...
3
votes
2answers
3k views

Making an ext3 filesystem image without mkfs.ext3

On ubuntu I'm using the following to create a ext3 filesystem image system.img. dd if=/dev/zero of=./system.img bs=1000000 count=200 mkfs.ext3 ./system.img I'm attempting to do the same on android ...
4
votes
3answers
4k views

How to get subshell's PID in Korn Shell (equivalent of $BASHPID)

In bash you have this handy variable: $BASHPID wich always returns the currently running subshell's PID. How can I get a subshell's PID in ksh? For example see the code below: #!/usr/bin/ksh93 echo ...
3
votes
5answers
645 views

changing pattern of a text file

A text file has contents something like chair table pen desk Now i want it to be changed and stored in a variable say var as below ('chair','chair'),('table','table'),('pen','pen'),('desk','desk') ...
3
votes
2answers
1k views

zsh: excluding files from a pattern

Say I have the following files: |-- bar `-- foo |-- type_A_1 |-- type_A_2 |-- type_B_1 |-- type_B_2 |-- type_B_xx |-- type_B_xx `-- something_else I thought the ...
1
vote
1answer
375 views

Can I get the backup option of cp and mv to work with directories?

--backup[=CONTROL] make a backup of each existing destination file The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this ...
3
votes
2answers
1k views

Keep global variables values piping through functions

I wrote a little bash script using sed on some html pages to extract some urls. To avoid each time grabbing sed results in a variable then read it again I simply made 3 functions and piped together. ...
3
votes
2answers
2k views

Creating a variable with sed in ksh

I'm using Ksh88 on a Solaris 10 machine. If I do the following: foo="one two three" for i in $foo; do echo $i; done The script executes as expected: $ ./script.ksh one two three However if ...
0
votes
1answer
416 views

Shell script executing in the terminal but not from shell script file [duplicate]

Possible Duplicate: How can I make variables “exported” in a bash script stick around? I have a problem with executing script from file. When I type in command line ...
14
votes
3answers
19k views

Testing if a variable is empty in a shell script

I have seen the following technique used many times on many different shells, to test if a variable is empty: if [ "x$1" = "x" ]; then # Variable is empty fi Are there any advantages on using ...
4
votes
2answers
1k views

How to output file & ignoring lines that start with “?”?

I do svn status --show-updates and then I want to either Q1: ignore (not to display) lines that start with ? Q2: display only lines that start with * Note that there are few spaces before * occurs. ...
0
votes
2answers
239 views

Autentification on SSH connection in OneLine [duplicate]

Possible Duplicate: Shell Script for logging into a ssh server I need to connect via SSH to a remote server, however I must do this with out interactive mode and with out use Public keys ...
0
votes
1answer
721 views

Calling a script in ~/.profile stops my logging in

I placed a call to a bash script in my ~/.profile. The script executes a CLI program to dim my screen at login. However, Gnome doesn't continue to log me in to my standard desktop after running the ...
1
vote
3answers
539 views

Problem with bash script executed from website

I have just begun writing scripts in bash and I am putting some of them on my web server (whihc is located on my virtual Debian 6.0.4 machine). I have installed lighttpd on this machine.and when I ...
1
vote
1answer
77 views

Can someone clarify: 'case “:$PATH:” in' in script?

I am not an expert at Unix scripting. An answer to another question suggests using the following piece of code in a script: case ":$PATH:" in *:$HOME/mydir:*) echo it is in the path;; *) echo not ...
2
votes
2answers
678 views

Checking if PATH contains $HOME/mydir and adding it if not (all in a script) [duplicate]

Possible Duplicate: keep duplicates out of $PATH on source I am not an expert with Unix scripting. Some of my scripts are located in $HOME/mydir. Unfortunately, they are not accessible from ...
5
votes
1answer
2k views

Cannot open: Input/output error with tar command in a shell script

I am trying to create a tar ball in a shell script (I have enabled set -x), but I get an error: + cd /home5/mysite/public_html + TAR_DUMP=gypo_2012-02-18-03:51:15.tar.gz + echo 'Tar name: ...
3
votes
1answer
686 views

merging files and getting column values based on id field

bash-3.2$ cat sample.log sample.log.1 sample.log.2 ID COL1 COL2 COL4 1 col1 col2 col4 2 c1 c2 c4 3 co1 co2 co4 ID COL3 COL1 1 col3 col1 2 c3 c1 3 co3 co1 ID COL1 COL2 COL3 1 col1 ...
1
vote
3answers
2k views

Script to show number of files (by file type) in a directory?

I want to show this kind of statistics about a particular directory: $ myscript.sh .c 34265 files .h 17866 files .cbl 1804 files .pco 389 files [...] Is there a tool that can do this? Here is my ...
5
votes
2answers
4k views

Set Variable Environment Variables in bash (or other)

I want my script to read a file containing key/value pairs of environment variables to set, and then to set them. So far, I have this: #!/bin/bash cat $1 | while read kv do key=${kv%=*} ...
2
votes
1answer
149 views

Files with no .sh extension not considered for autocompletion in zsh

I have noticed that unless I add an .sh extension to my shell scripts, Zsh does not suggest them for autocompletion for execution despite having them in my PATH. I have the same problem if I cd into ...
11
votes
3answers
17k views

File extensions for unix shell scripts

On wikipedia, the article for .sh says: For the .sh file extension type, see Bourne shell. How about other unix shells? I know that the shebang is used inside the file to indicate an ...
21
votes
7answers
3k views

What purpose does the colon builtin serve?

I've hacked on a lot of shell scripts, and sometimes the simplest things baffle me. Today I ran across a script that made extensive use of the : (colon) bash builtin. The documenation seems simple ...
1
vote
2answers
472 views

Proper way to use shebang for bash

What is the recommended options in bash's shebang? #!/bin/bash -e I currently have it as above. This sources $HOME/.bashrc. I don't want to source user's .bashrc/.profile as it might interfere ...
2
votes
1answer
703 views

What's wrong with this csh file?

I don't have any experience writing .sh files. I want to run the pvm2raw utility of this app from the Volume Library. I get the following error when running build.sh in Ubuntu 10.10. build.sh is ...
4
votes
4answers
3k views

How can I pass a command line argument into a shell script?

I know that shell scripts just run commands as if they were executed in at the command prompt. I'd like to be able to run shell scripts as if they were functions... That is, taking an input value or ...
0
votes
3answers
9k views

How do I redirect command output to a file?

I have a command that I run in a folder that outputs MD5 hashes and filenames on the terminal: ls |sort -nr | xargs md5sum I need this output in a text file that I can download and compare to ...
3
votes
2answers
379 views

need shell script to grock a csv

Need some pointing in right direction on script to fetch and regex or sed. Site24x7 provides a URL with a CSV list of their source IP's used for monitoring. (they also provide other formats, CSV ...
3
votes
4answers
799 views

command for counting number of active cron jobs in crontabs

Problem statement I have 5 solaris boxes, some have Solaris 10 and some have Solaris 9. All of them have many cronjobs in their crontabs. I would like to know number of active cronjobs available, so ...
10
votes
2answers
217 views

How to start tailing a file that has not been yet created

I use tail to monitor the progress of jobs that I know will write their progress to disk. Almost always, I know which file they will create before they start running (the jobs are dispatched by a ...
5
votes
3answers
964 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
1answer
2k views

“echo 1 > /sys/block/sdX/device/delete” on all disks except predetermined list

I have a list of scsi disks that I need to remove. The list is considered random at best and changes from time to time. I want to remove everything except a predefined list that I have created. Let's ...
2
votes
2answers
1k views

Invert results for “for in * do”-loops [duplicate]

Possible Duplicate: How do I reverse a for loop? Is there an easy way to invert a for in loop? For a script I want to process files in their reverse order. So instead of: for file in ...
152
votes
10answers
16k views

In Bash, when to alias, when to script, and when to write a function?

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just ...
0
votes
1answer
548 views

How do you wrap executable commands so that they work in an alias or function?

I'm still getting around the quirks of shell scripting, but I feel like I'm getting a little more comfortable with it. I've been trying to create an alias to generate a tar file that I have to create ...
3
votes
0answers
487 views

How to improve this backup script? [closed]

I wrote this backup script. I run rsync, tar and a dump of mysql. I'd like to add some features like email alert or some kind of statistic. Here is the script: #!/bin/sh DATE=`date +%D-%T` ...
8
votes
3answers
6k views

find -exec in bash script with variable expansion

I'm trying to run a command similar to the one below in a bash script. It should search through all subfolders of $sourcedir and copy all files of a certain type to the root level of $targetdir. ...