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

learn more… | top users | synonyms (1)

3
votes
3answers
102 views

Why is my condition always false?

I tried to write a script which creates a user. First it needs to check if: root is running if user maybe already exists I Also tried to set the password equal to the username which was typed ...
8
votes
3answers
98 views

POSIX compliant way to work with a list of filenames possibly with whitespace

I have seen Bash scripting guides suggesting the use of array for working with filenames containing whitespace. DashAsBinSh however suggests that arrays are not portable so I am looking for a POSIX ...
1
vote
2answers
76 views

How to invoke a shell built-in explicitly?

I want to customize the functionality of cd command as per my needs. I define following function - function cd () { cd "$@" && pushd "$@"; } The intent of this function is to automatically ...
3
votes
3answers
132 views

How to rename multiple files in single command or script in Unix? [duplicate]

I have the below list of files aro_tty-mIF-45875564pmo_opt aro_tty-mIF-45875664pmo_opt aro_tty-mIF-45875964pmo_opt aro_tty-mIF-45875514pmo_opt aro_tty-mIF-45875524pmo_opt that I need to rename to ...
3
votes
1answer
80 views

script to remember dir and always cd to it instead of the root dir

How can write a script to change to a given directory but also remember it so that when you do cd it always changes to that directory ? #!/bin/bash setdir() { cd $1 # remember the directory ...
5
votes
3answers
227 views

Using `kill -s 0 $pid` vs `[[ -e /proc/$pid ]]` to detect if PID is running

One of the idioms often used to check if process is running is to use kill -s 0 $pid. My question is, does it have any upsides over using [[ -e /proc/$pid ]] construct? The script I'm writing is ...
4
votes
2answers
92 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 ...
1
vote
1answer
40 views

auto-delete fluxbox menu entries

I want to delete fluxbox menu commands that are in the form of - [exec] (foobar) {/usr/lib/xscreensaver/foobar -root} and there are 50+ commands to be deleted. But, how do I do it please? I need a ...
2
votes
1answer
89 views

Can't echo or create a symbolic link to a file with special characters in its name inside bash script

I'm trying to create a symbolic link to files with the following naming convention: .$abc$namehere.zip;1 As can be seen, it has the following special characters in it: . $ ; In my bash script I ...
4
votes
2answers
185 views

Get the PID of file creation

I am writing a bash script that creates 5 files with the extension ".P" and write a line of "something" into them. "P" represents a number and it's the PID of the process that creates the file. How ...
10
votes
2answers
766 views

find and remove duplicates in a directory

I have a directory with multiple img files and some of them are identical but they all have different names. I need to remove duplicates but with no external tools only with a bash script. I'm a ...
-4
votes
1answer
29 views

Need help with this script [closed]

I'm supposed to write a bash script that has as an argument a path to a directory which contains a file named checking.txt. It needs to return the following exit codes: 4, if the directory doesn't ...
0
votes
0answers
79 views

Shell Script : Check for all the source files in different paths

This is the naming convention of the file. <yearmonthdate>_<sourcesystem>_<totalnoofpartsof file>_<subject area>_<part number>.dat (or .dat.gz) First part is Date, ...
0
votes
1answer
112 views

Meaning of different options used while running a shell script

I was reading a shell script and I found the below command there. I tried searching on Google thinking attach, force and delete are the options used while running a script. But failed to find ...
4
votes
4answers
284 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?
2
votes
3answers
115 views

How can I tell echo/printf in Bash to swallow escape codes, based on a condition?

I have a Bash script that is being run interactively and as a cron job. When run interactively it outputs colored text on the terminal. However, when run in cron it doesn't have a terminal and ...
5
votes
3answers
200 views

removing redundancy from output columns

What is a *NIX way of removing redundancy in a case where I have pairwise comparisons like these in two columns A B B A A C A D C A D A B C C B A B and B A ...
-2
votes
1answer
75 views

How to create shell script command so that it will self-detruct after performing it's task? [closed]

I want to make command run in Ubuntu 13.10 as soon as I login. I know for that I have to add certain command in ~/.profile file. That's not the problem. I want to certain line of that ~/.profile ...
1
vote
3answers
137 views

Filtering a result from a command

I am having trouble writing a little program; when I run the following command from my program: gsettings get org.gnome.desktop.background picture-uri I get: ...
3
votes
1answer
421 views

How to write bash script to configure my displays when HDMI is plugged in or unplugged

I have a notebook running Kubuntu Precise (12.04) which I occasionally use for watching videos. When I do, I plug in an HDMI cable connected to an A/V receiver with an HDMI monitor attached to it. ...
1
vote
1answer
113 views

Grep Wvdial output while it is running

I want to grep the output from wvdial while wvdial is running. Actually I want to fire a task when the modem is connected. I want to do this in the following way: grep string "IP address", if it is ...
0
votes
0answers
20 views

file system is not mounted on system: warning crnonshdw not mounted on system

I am new to this field scripting. I am a Solaris engineer. In my system when I type: df -k it will output it like this: mmr1ap04:/xahome/xa21> df -k Filesystem kbytes used avail ...
1
vote
3answers
99 views

Extracting mail addresses

I have a file that contains lines like this: user=<xxxxxxx@asd.com.tr>, user=<yyyyyyy@asd.com.tr>, user=<zzzzzzz@asd.com.tr>, user=<ccccccc@asd.com.tr>, ...
0
votes
1answer
147 views

need to display hard error , transport error & soft errors above 2

iostat -En | nawk '/Hard Errors/ {line=$0;gsub(/.*Hard Errors:/,""); \ if($1>2) printf("%s\n",line); }' The output of this is c0t1d0 Soft Errors: 0 ...
0
votes
0answers
10 views

need to display Hard error,transport error &soft error above 2 for the output of iostat -En | grep Hard like this [duplicate]

I am new to this field scripting. iam a solaris engineer.shall i get the answer of this script in KSH scripting" iostat -En | nawk '/Hard Errors/ {line=$0;gsub(/.*Hard Errors:/,"");if($1>2) ...
0
votes
2answers
266 views

Using `ls` recursively without -R

I am trying write a shell script that has output similar to ls -R without using the option -R. This is part of a programming assignment that forbids me from using -R, find and du. I need to identify ...
0
votes
1answer
86 views

In shell script, execute a command only if the source is newer than the target

I'm looking for a simplified version of this: dep=0 if [ ! -e targetfile ] then dep=1 elif [ targetfile -nt sourcefile ] then dep=1 fi if [ $dep -eq 0 ] then echo "Already up to date" ...
0
votes
1answer
301 views

Script for running sequential job

I started recently to learn using Linux. I want to write a script in bash shell to run a serial job on a cluster. I have been searching for hints and instructions on how to write such a script. I ...
1
vote
1answer
82 views

Execute command on remote host fails

I'm trying to execute some commands on remote host like this : ssh -t -t -l user 172.20.20.20 <<EOF pid=$(ps -ef | grep [a]pp-management | awk '{print $2}') && kill -9 $pid && ...
5
votes
3answers
572 views

Bash: interactive remote prompt

I have a script which connects to a remote server and check if some package is installed: ssh root@server 'bash -s' < myscript.sh myscript.sh: OUT=`rpm -qa | grep ntpdate` if [ "$OUT" != "" ] ; ...
1
vote
1answer
94 views

Copying wallpaper images into a single folder

In my ubuntu 12.04 set-up, there are lots of wallpaper images in the directory /usr/share/wallpapers. For example, one might be usr/share/wallpapers/Leafs_Labyrinth/contents/images/1600x1200.jpg ...
1
vote
2answers
129 views

Count entries for each directory in tree

Say I have the following tree: root: d foo d bar /foo: d A - tmp.txt /bar: d B - tmp2.txt /foo/A: - tmp3.txt /bar/B: d = directory and - = file I need to answer the following question: which ...
2
votes
5answers
105 views

Read contents in a line in bash

I have a file with contents in this order which is separated by space 4096 1230 4560 1024.... n How can i read each number and pass it on to another command, eg divide each number by 1024.
3
votes
1answer
115 views

Simple Bash Script

I have a simple problem, but unfortunately I don't know where to even start (I'm just starting out). So, what I want to do is ultimately increase my vocabulary. I got the idea to strip the most ...
2
votes
1answer
39 views

Linux shell - unexpected error

I'm new to the linux & command world. I have two directories, one called pasta (which contains 3 files) and another one called pasta2 (with nothing inside). I'm trying to move all 3 files from ...
5
votes
1answer
112 views

Variable in sed

I have a file that contains filenames with absolute path. In the middle of that path, is a variable directory, whenever I hard code the variable part, it works fine, but when I replace it with the ...
3
votes
4answers
251 views

How to prepare files for rsync on a case insensitive filesystem?

I am transferring a large number of files on a HFS+ filesystem. The files are currently on ext2 partitions. I have conflicts due to case insensitivity of the target partition (HFS+). I want to ...
2
votes
3answers
84 views

What is the language that appears on the first line of a script? [duplicate]

Bash scripts start with the following line #!/bin/bash # Rest of script below ... In bash the # character is the start of a comment, but #!/bin/bash is definitely not a comment, therefore it isn't ...
3
votes
2answers
119 views

How to make /etc/init.d script act like it's launched under X?

I'm trying to track down some quirkiness with a Java application that misbehaves when launched via a startup script in /etc/init.d , but runs fine when you open a GUI terminal window and run it ...
2
votes
1answer
173 views

Linux nuisance: /dev/stdin doesn't work with sockets

Linux has this blitheringly annoying peculiarity that /dev/stdin won't work with sockets - it is hardcoded to return ENXIO. Try this: socat TCP-OPEN:localhost:1234 EXEC:cat\ /dev/stdin,nofork ...
4
votes
3answers
338 views

How do I create a bash script that sums any number of command line arguments?

How would I create a bash script that the user can use to sum any amount of command line arguments? For example, say my script is called sum: sum 3 3 sum 3 5 8 sum 9 8 21 38 And so on.. I ...
4
votes
3answers
176 views

How to start a script with clean environment?

I tried the following but it doesn't seem to work: $ cat script.sh #!/bin/env -i /bin/sh /bin/env $ script.sh /bin/env: invalid option -- ' ' Try `/bin/env --help' for more information.
1
vote
3answers
198 views

compare files line by line and create new one bash programming

I have two text files. File 2 has logs over 1,000,000. File 1 has IP addresses line by line. I want to read file 2 lines and search these lines in file 1, I mean: file 1: 34.123.21.32 45.231.43.21 ...
3
votes
4answers
200 views

Does cron jobs per se represent a significant burden on a modern system?

I'd like to run a pretty simple script (is there a dir with name x in dir y if so move it to dir z–and x will only be there about once a day) every 15 seconds or so (one minute divided into 4*15 ...
3
votes
1answer
150 views

echo $? returning 0 instead of 1

[root@server ~]# bash << eof > cat adsfasdf > echo $? > eof cat: adsfasdf: No such file or directory 0 Could anyone please explain why it is returning exit status 0 instead of 1?
1
vote
2answers
168 views

run a script at shutdown in Debain

I am using Debain wheezy. My problem is , I am using a limited 2.5 GB net connection. My idea is simply running a script as #!/bin/bash date >> ~/mydatausage.txt ifconfig -a >> ...
1
vote
1answer
74 views

Find a particular nesting of directory names, e.g. …/project/dir1/dir2

I'm having some issues dealing with the find command; I don't know the syntax for it very well and I need to use it in a script to find a specific directory structure that could be located anywhere. ...
1
vote
1answer
141 views

Run an executable with a Shell Script

I have mplayer installed and I want to create a simple shell script that runs in bash and executes mplayer * in the current directory. So, how to call upon mplayer in the shell script? I am a newbie ...
3
votes
1answer
77 views

Bash capture any error like -e but don't exit, do something else

I want to set a flag in a shell script (bash) so that if anything returns non-zero, a flag is set (i.e. set a variable like errors="True"). So far, I've thought of calling the script with ...
1
vote
1answer
143 views

Exiting a Bash script when a sudo child quits

I'm trying to launch a couple of sub-programs from a bash script and then wait on either of them to quit before quitting the other and exiting the script. CMD1PID= CMD2PID= exit_trap () { ...