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

learn more… | top users | synonyms (2)

4
votes
3answers
241 views

shell script problem with using the operators

I am comparing two strings using the levenshtein distance algorithm. I got the PHP implementation of the algorithm from here. I am calling this php implementation from my shell script as below. ...
4
votes
2answers
364 views

Dump process's stdin and stdout

I have two processes, let's say Parent and Child. Parent launches the Child and communicates with it through child's stdin and stdout. Parent <-> Child These processes use text protocol and I ...
4
votes
3answers
1k views

Help Shell script to pass variables to rsync

I'm trying to create a simple wrapper shell script for rsync. When I send file names to my script, rsync can never seem to identify the correct location. The file names have spaces in them. I've tried ...
4
votes
2answers
3k views

Insert to oracle table in a loop

Requirement: In one server in a directory multiple files are present. I need to read the file names and populate into a oracle table. I have written the script like this, but its not working: ...
4
votes
2answers
664 views

Create sub-directories and organize files by date from file name

I have been using the following script to organize my photos into "Date' Directories: for x in *.JPG; do d=$(date -r "$x" +%Y-%m-%d) mkdir -p "$d" mv -- "$x" "$d/" done This script works ...
4
votes
3answers
1k views

using sed to replace pattern with hash values

I want to search the file and replace specific pattern with its hash (SHA1) values. For example, let file.txt has the following content: one S56G one two three four five V67X six and I want to ...
4
votes
3answers
15k views

Loop over a file and read values from two columns into variables

I have a file (data.dat) which contains 3 columns. I want to read in two of them with the shell script and fill the contents into a template file. The file looks like this: 12 180 2390 14 177 2210 16 ...
4
votes
2answers
26k views

Assigning the output of a SQL query to variable

I am connecting to oracle database and firing a query and assigning the output to variable But when I echo the value of the variable it doesn't get printed correctly. count=`sqlplus -s ...
4
votes
3answers
284 views

Back up logs to a new directory

I need help to write a script to copy log files. There are two format of logs: System_<date_timestamp>.log trace_<date_timestamp>.log New files are generated when trace log reaches ...
4
votes
2answers
2k views

Specifying a generic interpreter for a program like expect?

I am writing expect script which can be used on mulple operating systems but the problem is i can't use #!/usr/bin/expect evreywhere so instead i tried to do #!`which expect` at the top but it ...
4
votes
2answers
10k views

list all files newer than given timestamp and sort them

I want to list all files (sorted by date) that are newer than timestamp in format 20130207003851 in directory /tmp only. Subdirectories can be omitted. Using SUSE Linux Enterprise Server 11. The ...
4
votes
2answers
8k views

How to remove trailing whitespace at the end of the line in given files (more than one)?

I'm sooo fed up with useless spaces in source files that I've even configured vim to show them. The problem is that I'm very often (if not always) have to deal with useless spaces with my mates ...
4
votes
2answers
5k views

How to write a shell script that gets executed on login in?

I am trying to write bash shell script in Ubuntu 11.10 Linux distro, that will get executed automatically on logging into the system. But I am not able to figure out that what to write in script that ...
4
votes
1answer
2k views

How to create a confirmation question in Linux?

I have a certain command (git push server-name) that has major consequences. How to require confirmation for this command only? It should ignore white space. The confirmation could be Enter 'yes i am ...
4
votes
3answers
1k views

What's the best way to determine the working directory for UNIX process using 'ps'?

I am running a Ruby command line script (rufus.sh)which ultimately calls Thread.new, which spawns a UNIX process as shown below. I run this script for more than 1 directory as the output of the ps ...
4
votes
2answers
536 views

Shell script printing the most visited directories

Is there a way to write a bash script with the following functionalities? Be launched when I press some key or key combination. (not so important requirement) Identify the 7 most visited directories ...
4
votes
3answers
280 views

Best way to work through / display a tree of images sorted by size

I've got a deep directory tree containing .PNG files. I'd like to find all the .PNG files in the directory, sort them in order of size from smallest to largest, and then display every 50th image. ...
4
votes
6answers
185 views

Loop in text files shell script

I have a list of files in one directory and a set of jpegs corresponding to each file in another directory. I need to loop over all files, and for each file name, determine the target directory. For ...
4
votes
1answer
48 views

Settings environment variables from a file

I want to have a file (outside of git) that contains certain environment variables. While a script (which is in git) needs to read them and export to env. My current implementation works for ...
4
votes
4answers
275 views

How can I copy a file in a bash script and rename it while copying and place it in the same directory

How would I copy a file "file.doc" and rename it while copying to "file_copy.doc" and place it in the same directory ? And this only by calling the script and adding the file name in the argument: ...
4
votes
2answers
128 views

Shell script error handling while assigning STDOUT to variable

I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash? My script looks like this: #!/bin/bash try() { "$@" code=$? ...
4
votes
1answer
181 views

How to catch a signal in command line?

I asked this question. SIGPIPE signal is generated to stop the execution of the command as told in the answer. But how do I capture this signal and gracefully terminate the command? The command exits ...
4
votes
1answer
582 views

Bash script for scp interpreting tilde (~) too soon

So, I just started a new C++ class that requires me to program in a Linux environment. I don't have Linux installed on my machine, but I do have OS X and I can ssh into a linux machine in the lab at ...
4
votes
1answer
3k views

Bash Script Permission denied & Bad Interpreter

I'm on a kali linux 64 bit. I have created a python script which takes 2 arguments to start. I don't want to type out every time the exact same paths or search in the history of the commands I used ...
4
votes
1answer
1k views

Variable scope in while-read-loop on Solaris

Could someone please explain to me why my while loop seems to have an internal scope? I've seen multiple explanations online but they all have to do with pipes. My code has none. The code: ...
4
votes
2answers
242 views

How do you correctly extract all command synopses from manpages in /usr/share/man/man1?

I'm trying to extract all command synopses from manpages in /usr/share/man/man1 using: #!/usr/bin/env bash ## synopses - extract all synopses in /usr/share/man/man1 cd /usr/share/man/man1 for i in ...
4
votes
2answers
2k views

How to kill a process started as daemon?

I start a process using foo &. It outputs something like: [1] 16915 This is the process id, I guess. How can I store this number into a variable named MY_PROCESS_ID (for example)? Then I ...
4
votes
3answers
1k views

purpose of run-parts(8) utility

At least under Debian-based systems, there is a run-parts utility in debianutils package which is used in various scripts. For example in /etc/X11/Xsession. It will run all the executable files found ...
4
votes
2answers
686 views

Shell Script to isolate files scattered in different folders

I need to write a shell script that traverses a given directory structure in multiple levels, recognizes the video files scattered in the folders and copies all of them to another single folder. The ...
4
votes
1answer
1k views

how to make my bash script look 100% like a GUI app (so user never sees terminal)

I created a bash script using kdialog (and it runs on Kubuntu 12.04). The GUI is used for every user interaction (after the first one). However, the script still has to be launched from the terminal ...
4
votes
1answer
519 views

Prompt for confirmation for every command

I'm writing a pretty ad-hoc install script for some thing. No much control constructs, basically just a list of commands. I'd like the user to confirm each command before it gets executed. Is there a ...
4
votes
1answer
8k views

error handling in expect?

I am just improving my quetion becasue i have acieved so far upto this: set username [lindex $argv 0] set password [lindex $argv 1] set hostname [lindex $argv 2] if {[llength $argv] == 0} { ...
4
votes
2answers
413 views

Is it possible to store ssh connection in bash script instead of $(ssh user@ip command) every time?

I think that the code that I already have is not very efficient as it has to connect each time to the same machine and execute a command. Code: tmp=$(ssh -nq $USER@$IP "$COMMAND" 2>> $LOG) ...
4
votes
2answers
317 views

Shell script to backup directories

I'm running eclipse on windows and would like to automatically backup my src direcories for all my projects. I installed cygwin and tried to make a simple backup script but I am having trouble ...
4
votes
2answers
554 views

Download from dynamically changing urls via script

I want to write a script to download all Walter Lewin lectures from this page in wmv format: http://videolectures.net/mit801f99_physics_classical_mechanics/ However the links to the wmv file for ...
4
votes
1answer
457 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?
4
votes
1answer
3k views

Recursively compare directory contents by name, ignoring file extensions

I have a directory containing about 7,000 music files. I used lame to recursively re-encode all files in it to a separate directory, outputting all files with the same relative path and file name. ...
4
votes
2answers
85 views

Copy the files automatically to destination as soon as created

Is there any way to copy the file in Unix as soon as it is created? I have one location where files will be created in different timings.How can I copy the files automatically using shell script to ...
4
votes
4answers
72 views

Traverse, copy & transform file names

My issue: I need to copy log files, all with identical names, but stored 1-deep in subdirectories, and change their names. I'm copying from the a folder called Logfiles which contains a bunch of ...
4
votes
1answer
114 views

How to perform the watch command onto expression with pipes?

I learned today the wonderful shuf command: ls | shuf shows me a listing of the working direcotry, but thanks to shuf each time I execute this piped command expression with another order. So I ...
4
votes
2answers
79 views

How do I convert a string into a number interpreted in certain base in bash script?

I'm simply trying to convert a string $RECORD_HDR to a number $RECORD_SIZE, where echo $RECORD_HDR gives 003D and echo $RECORD_SIZE should give 61
4
votes
2answers
156 views

When is 'if' not necessary?

In some scripts, I've seen if conditional statements written as: if [ some_conditional_statement ]; then # do stuff fi or [ some_conditional_statement ] && #do stuff Is the second ...
4
votes
2answers
54 views

More shell scripts than one in one text file?

I am learning shell scripting for my introductory UNIX course at my university, and I`m curious if is it possible to have more than one shell script in one text file. What I mean by that is, for ...
4
votes
3answers
5k views

Running commands on multiple servers [duplicate]

I want to execute a process on multiple servers. I am using the following wrapper script. The script moves to servers one at a time. I would like to change it so it can execute on multiple servers. ...
4
votes
4answers
156 views

Renaming multiple files with rename

I need help renaming files in batch--the other questions I browsed don't exactly address my problem. The names of my files are generated non-deterministically, so I can't predict what they will be ...
4
votes
1answer
244 views

Proper way to distribute shell scripts

What is the most proper way to distribute shell scripts, if behaviours of shells can be modified by set and thus are unpredictable? For example, rm *.txt wouldn't be executed as expected on the ...
4
votes
2answers
48 views

How to sort a collection of lines from different files?

I want to sort second line in each file by second column, and also print name of the corresponding file. I am doing it like this - rm tmp; for filename in file*; do num=`head -2 $filename | tail ...
4
votes
2answers
455 views

Launching a terminal emulator without knowing which ones are installed

I need to write a script that will launch a terminal emulator and run a particular command in it. Since I don't know which terminal emulators have been installed on the target system, how could I find ...
4
votes
2answers
121 views

Remove specific file from directory if it's the only one

I have a lot of directories and sub-directories. I want to recursively remove one specific file from a directory if it's the only one there. Also assume that there are no sub-directories in that ...
4
votes
1answer
2k views

Automatically detect when a file has reached a size limit

I would like to send an email when a file reach a certain size limit. The only way I thought of doing this is by doing a cronjob which will check the file size and send the email if the file is ...