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

learn more… | top users | synonyms (2)

4
votes
3answers
265 views

How can I test if a variable is empty or contains only spaces?

The following bash syntax verifies if param isn't empty: [[ ! -z $param ]] For example: param="" [[ ! -z $param ]] && echo "I am not zero" No output and its fine. But when param ...
2
votes
1answer
11 views

How to append multiple lines to a file with bash, with “--” in front of string

Question very similar to How to append multiple lines to a file with bash but I want to start the file with --, and also append to the file, if possible. printf ...
1
vote
3answers
31 views

moving a (file | directory) while avoiding filename collisions

I have a bash script that moves files from a number of different locations to a folder named completed. I want to avoid overwriting previous files, so in the case when the name of a file (for ...
0
votes
3answers
19 views

waiting for network in a bash script

I'm running a script that relies on network being up and a network share be mounted. The script runs on login (which happens automatically after boot). The problem is that by the time the script runs, ...
3
votes
0answers
12 views

Get notified from avahi

After looking for a zero-conf browser valid implementation I realised that inside my linux machine (ubuntu) I already have avahi and typing: avahi-browse -alr I get a complete list of all devices ...
1
vote
2answers
40 views

Unix Shell Scripting output to an Excel File

I have a Shell script with Multiple SQL statements like this: select Count(*) from Emp where date= current_date-1; After executing the Shell script i need a MS excel file to be create with Column ...
12
votes
5answers
4k views

How can I get bash to exit on backtick failure in a similar way to pipefail?

So I like to harden my bash scripts wherever I can (and when not able to delegate to a language like Python/Ruby) to ensure errors do not go uncaught. In that vein I have a strict.sh, which contains ...
1
vote
1answer
10 views

Make some fields required in dialog

The script below creates a dialog box. The user can press Enter at any time, and then the script continues. I need to block the Enter key to ensure that the user fills all the fields. How can I do ...
2
votes
3answers
75 views

Count files in a directory by extension

For the purpose of testing, I'd like count how many images files are inside a directory, separating each image file type by file extension (jpg="yes". This because later it will be useful for another ...
5
votes
1answer
1k views

Global /etc/profile setenv or export overwriting user settings in .profile

I need to call a modified version of a program in /home/user_name/bin rather than the (older) version in /opt/program_name/current on a CentOS 5.3 cluster. The program creates a tcsh script using ...
1
vote
1answer
468 views

Command/scripts to check the Reverse DNS Lookup time

I am working on a project in which one master communicates with numbers of slaves. For that it has to make connection with hosts in network. But sometimes it hangs. I think that the reason behind is ...
1
vote
1answer
618 views

Deleting Non-empty Directory with smbclient

How can I remove a non-empty directory with smbclient? I have tried the following: 1) rm Result: NT_STATUS_NO_SUCH_FILE listing [dir_path] 2) recurse;rm Result: No Error but also nothing ...
-2
votes
2answers
163 views

How to get the port number a program is listening on in a shell script? [closed]

I have a program (beam) running, and I need to figure out which TCP / IPv6 port it is listening on. I know netstat will tell me, but I haven't fully been able to parse its output: ...
3
votes
5answers
48 views

reading a list of lines from a file writing output to a utility

I have a file with a list of lines I want to read from another file. I want to output those lines to a utility (grep) that lets me read the entire line and pull information from it. The file with the ...
2
votes
0answers
14 views

Running shell script inside with vagrant provision

I have setup a vagrant box using precise64 (ubuntu 14.04) and installed packages needed for octopress. I can ssh into the vagrant box and generate posts and run preview for my octopress site. Now I ...
4
votes
1answer
100 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
35 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 ...
-1
votes
0answers
45 views

Unique id for combination of Latitude - Longitude

I have a LibreOffice Calc table containing one column with latitude and one with longitude. What I want to do is for every unique combination of latitude and longitude a unique number to be assigned ...
2
votes
1answer
35 views

Arrow key/Enter menu

I need help finding out how to possibly create a menu in a shell script that will display 3 options that a user will use the arrows keys to move the highlight cursor and press enter to select one. If ...
2
votes
2answers
74 views

How to clear the input in Bash

I have a bash script that is reading quite a bit of data /dev/random. I need the user to type randomly to seed the random daemon. However once the script completes everything typed will get dumped to ...
1
vote
1answer
23 views

bash script not being run by cron [duplicate]

I have a very simple bash script that runs flawlessly when I do ./removeOldBackup.sh or sh /home/myusername/backup/removeOldBackup.sh but when I add it to crontab like * * * * * sh ...
1
vote
1answer
42 views

Memory buffer in a bash script

I have recently finished a script that will ask for a hostname and automatically take the argument of the function and ssh into one of my work servers. Here is a copy of my script: #!/bin/bash echo ...
2
votes
0answers
27 views

Shell scripts for the database logs

The actual log file will be like this $ cat file1.log time=2014-07-23 23:56:28 GMT, user=[unknown], db=[unknown], host= pid=28254 LOG: time=2014-07-23 23:56:28 GMT, user=portalman, db=ss, ...
1
vote
0answers
20 views

Trap not trapping SIGTSTP

I have a script that keeps displaying the login prompt: #!/bin/bash trap '' 2 20 while true; do login done This is part of a bigger project so please don't tell me to just use the system login ...
6
votes
7answers
198 views

Count total number of lines before/after a pattern match

I am having a long list of IP addresses, which are not in sequence. I need to find how many IP addresses are there before/after a particular IP address. How can I achieve this?
0
votes
1answer
1k views

Why does scp say “No such file”?

I have a bash script that uses scp to copy a file from my machine to another. The script keeps on exiting after I enter the SSH password with the error: <filename>: No such file or directory ...
3
votes
3answers
51 views

How to read a text file line by line which is not constant in shell scripting

I need to read a text file line by line and the lines are not constant it may vary.For example $ cat file1.txt syntax error at or near "?" at character 1 syntax error at or near "[" at character 1 ...
3
votes
2answers
34 views

fdupes - delete files after comparing two directories

I'm currently trying to solve an issue using fdupes. I'd like to compare two folders with each other, and afterwards delete all duplicate files within one of these directories. Example: Files are ...
8
votes
3answers
369 views

Creating a Bash script that will execute a program and if it exits without crashing, will run it again

I'd like to know the easiest way of creating a bash script that will run an app in MacOSX (it's a UNIX based system so I assumed this was the right place). wait for the program to exit. If the exit ...
6
votes
2answers
155 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)
1
vote
3answers
3k views

Print given number in reverse order

clear echo "Enter a number" read n sd=0 rev=0 while [ $n -gt 0 ] do sd=$(( $n % 10 )) rev=$(( $rev *\ 10 + $sd )) n=$(( $n / 10 )) done echo "Reverse number of entered digit is $rev" ...
-2
votes
0answers
14 views

How to apply label in clearcase through unix [on hold]

I'm new to scripting. I came across this, I have to create a label for file while checking out I tried command mklabel but it did not work for me. I think I'm not using it in the right format. I work ...
1
vote
2answers
165 views

How to iterate through multiple file extension without caring about case sensitivity?

I've seen various topics on how to iterate through multiple file extensions but in most case, the list is defined. example: for file in ${arg}/**/*.{txt,h,py} do .... done As can be seen, ...
12
votes
6answers
10k views

How to compare to floating point number in a shell script

I want to compare two floating point numbers in a shell script. The following code is not working: #!/bin/bash min=12.45 val=10.35 if (( $val < $min )) ; then min=$val fi echo $min
8
votes
4answers
301 views

Do you prefer bash scripts or aliases for shortcuts?

I have a ~/bin directory (which is on my PATH) where I store a lot of little 1 or 2 line scripts. Some of them just cd into a directory and run a command on a file, like vim or something. But I also ...
2
votes
1answer
46 views

How to set parameter zero ($0) while executing a script?

I can manipulate positional parameters using shift or set -- while a script is interpreted. How can I change $0?
5
votes
3answers
178 views

How to iterate through a list of files with spaces that is sorted (case insensitive)?

I am doing this in OSX which uses bash but obviously, not all bash conventions are used so hopefully your suggestion is available to me :) I have the following files fun bar1.txt Foo bar2.tXT ...
6
votes
3answers
330 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 ...
3
votes
1answer
39 views

How do I use sudo to run multiple commands as another user within a portion of a script?

I'm trying to run the following: if [! -f .owner ]; then creator=$(whoami) sudo -u appuser ksh echo "$creator" >> .owner mkdir test1 test2 test3 exit fi However, the ...
-4
votes
2answers
86 views

“bash: [: : integer expression expected” [closed]

I want this function to return one random word if there aren't any command-line arguments. I am modifying linuxconfig.org's random-word generator to run even when #$ -ne 1. function random-word { ...
3
votes
1answer
35 views

Triggering Bash Before Killed [closed]

I just made some kind of service code. Check this out: do_start() { while true do if [[ $(pgrep -fl loopsvn.sh | wc -l) -gt 2 ]]; then echo "Already use by another ...
5
votes
1answer
551 views

Noninteractive script running over ssh is interrupted as apt-get finishes

When I run the following non interactive script on Ubuntu Server 13.04 it's interrupted when lxc-docker package finishes installation. Script : ssh -o StrictHostKeychecking=no -t -t -i $CERT ...
10
votes
4answers
607 views

Recursively create directories for all letters

I want to create a directory in such a way that I need to label the directories from a to z. Inside each of these directories, I need to create sub-directories so that they are labelled as aa, ab etc. ...
2
votes
1answer
25 views

Fetching Remote Windows System Information From Linux Server

I need a script/command that would fetch Windows System Information (Host Name,User Name, Domain, OS Info, Hardware Info, Memory Info, processor Info, etc) by running it on a Linux Server (RHEL 6.5 ...
3
votes
2answers
59 views

transfer a file to remote dir only if doesn't exist without replacing

I'm using CentOS linux. In one line cmd, that returns 0 if success, non-0 if failure, I need: Transfer a file to a remote server. Don't transfer the file if filename exists in remote server ...
-2
votes
0answers
11 views

Watchdog TeamCity Server using a shell script [closed]

I'm trying to write a script that automatically restarts Team City (a continuous integration tool). So far I have this: RESULT=`netstat -na | grep 8111 | awk '{print $7}' | wc -l` if [ "$RESULT" = 0 ...
-1
votes
1answer
25 views

add groups from external file

I want to write a script which add groups to the system, specified in groups.txt file. content of groups.txt : group01 : computer group02 : electronics group03 : IT . ...
2
votes
6answers
13k views

How to compare two dates in a shell

How to compare two dates in a shell script? For example something like todate=2013-07-18 cond=2013-07-15 if [ $todate -ge $cond ]; then break fi It doesn't work. ...
4
votes
6answers
177 views

Finding the number of time a particular number in a file where range also specified

I have a file with numbers separated by ,(comma). In between it also contains a number range like 300-400. Say for example I have a text file, namely testme.txt which looks like, ...
11
votes
3answers
422 views

Comparing two strings in Bash

I have the following if block in my bash script: if [ ${PACKAGENAME} -eq kakadu-v6_4-00902C ]; then echo "successfully entered if block!!" fi The script execution is not entering my if block even ...