The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …).

learn more… | top users | synonyms (1)

3
votes
2answers
66 views

How to use getopt in bash command line with only long options?

There is a getopt command in bash command line. getopt can used with short options (such as getopt -o axby "$@"), and can be used with both short and long options (such as getopt -o axby -l long-key ...
1
vote
1answer
14 views

Print difference of column1 if column2 values are same

I have a file something like this: 19:32:19 4599544 19:32:22 4599544 19:33:07 4599545 19:33:11 4599545 19:33:58 4599546 19:34:01 4599546 1st column is ...
110
votes
10answers
36k views

Turn off buffering in pipe

I have a script which calls two commands: long_running_command | print_progress The long_running_command prints a progress but I'm unhappy with it. I'm using print_progress to make it more nice ...
0
votes
1answer
24 views

Howto adapt this bash construct to a sh valid construct

On converting some bash scripts to a platform where only /bin/sh is available i'm stumble about this bash construct: #!/bin/sh RSTPD=/sbin/rstpd RSTPCTL=/sbin/rstpctl RSTP=0 ...
0
votes
0answers
16 views

Unix Shell script to delete all traces of itself after execution through crontab

My Question has 2 parts. Help with both parts is greatly appreciated. GIVEN INFO: - Lets assume there are 2 Folders, Folder A and Folder B. - Folder A contains many files. - Folder B contains the ...
1
vote
0answers
21 views

TCPDump To Capture A Tracepath6 Datagram

I'm trying to use TCPDump to capture an IPv6 datagram using: tcpdump -c 1 -i eth1 -vvvs0 ip6 and dst host H2.net2 & tracepath6 H2.net2 I'm running this script on H1.net1; R12.net1 is the ...
1
vote
2answers
738 views

The command ` ls -ltu ` fails to list folders/files based on last accessed time

First on your linux desktop create 2 folders. a b Now run this in the terminal ls -ltu the result is drwxr-xr-x 2 root root 4096 Aug 30 20:33 b drwxr-xr-x 2 root root 4096 Aug 30 20:33 a ...
1
vote
1answer
500 views

Can I use pmap to find out Memory utilization?

I want to measure memory utilization of my Apache Server. Can I use the pmap command and pass all the process IDs of httpd and take that as total utilization? pmap PID1 PID2 .... It seems that pmap ...
4
votes
3answers
1k views

How to pass arguments to bash shell script when executing that command with at?

I have a shell script that accepts an argument and uses that in the script. So to run the script I type /path/to/script argument The problem is when trying to setup that file to run using the at ...
0
votes
1answer
20 views

saving contents of echo output to variable in non-bash script

I'm used to doing something like this in my bash scripts to echo output to a variable: myvar=`echo $OSTYPE` echo "my os is of type: ${myvar}" On this current project however I'm using sh, which on ...
-2
votes
0answers
33 views

How do I automate this in a shell script?

I'm using the application PlexConnect on my home server (a headless OSX box) and I have to ssh in (from an iMac) and manually kill the Python process sometimes with the following: ssh 192.168.0.7 ...
2
votes
2answers
169 views

Shell script written in a different shell than what my current shell

If the shell script is #!/bin/sh But the value of my $SHELL is ksh. Will it make a difference if I changed my ksh to sh, and then executed the script. ie. will a script have different behaviour ...
1
vote
1answer
33 views

command not found via shell script but works on terminal

In my .sh file I cannot invoke ant or mv or cp commands but the same commands executes on terminal.below is my script sample.sh file #! /bin/sh cp filename.so filename_org.so android update project ...
51
votes
13answers
46k views

How can I get distribution name and version number in a simple shell script?

I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the ...
8
votes
4answers
374 views

Regular expression using \\ vs using \

Why does grep e\\.g\\. <<< "this is an e.g. wow" and grep e\.g\. <<< "this is an e.g. wow" do the same thing? If I add a third slash, it also has the same result. BUT, once I ...
1
vote
1answer
21 views

several virtual profiles for the same user

Many times there are several "identities" which one user uses: for work, open-source, academia etc., all require different ssh keys, editor configurations, git configs etc. I was wondering if there ...
5
votes
3answers
753 views

What does “if !” mean?

I came across a if/then statement like this: if ! foo ; then echo "blah" exit 1 fi What specifically does if ! mean? "If the result of foo is not true?" "If the exit code of foo ...
7
votes
3answers
4k views

What is the most correct way to pass an array to a function?

Consider I have a very large array $large_list, is there a way to write a function that will take the array as an argument? For example: echo_idx_array () { arr="$1" idx="$2" echo ...
1
vote
3answers
88 views

How to enter foo-* folder without actually using wildcard or shell expansions?

Short story I'm looking for the command to enter first found foo-something directory like: cd foo-* but without using wildcard (or other special shell characters). Long story As part of the ...
-1
votes
0answers
32 views

Practical usage of pushd, popd, man, less, and cat in bash [on hold]

So i am learning more advanced usages of bash/cmdline due to work teaching me some programming and im trying to wrap my head around some of the following bash cmd's and their purposes. I have been ...
2
votes
1answer
74 views

How to pipe a command output to multiple programs

I'm using FFmpeg and I have made a custom application which is a TCP server. I 'm using duplicate output in ffmpeg. What I want is, for each output I want to start my program to listen on a specific ...
16
votes
4answers
10k views

Converting relative path to absolute path

Is there a *nix command to get absolute(and canonicalized) path from relative path(with current path) or symbolic link?
0
votes
1answer
617 views

Cant ssh, connection terminates immediately with exit status 254

The most recent thing I remember is changing the soft and hard memlock ulimit to unlimited. Now I cant ssh into the machine. This is the ssh log. Authenticated to IP ([IP]:22). debug1: channel 0: ...
-1
votes
1answer
48 views

Is it possible to run two commands at the same time in a shell script?

I know that on the command line I can use & to run a command in the background. But I'm wondering if I can do it in a script. I have a script like this: date_stamp=$(date +"%Y-%m-%d" ...
2
votes
2answers
38 views

Optional arguments after or before the mandatory arguments?

I'm creating a shell script and I need it to take two mandatory arguments and one optional. How do I check that? In most scripts I've seen, the optional arguments are passed before the mandatory ones, ...
2
votes
2answers
44 views

Environment variable not expanded inside the command line argument

I have a file user-pid.out2 which has "usernumber" and "process id" as two columns. based on usernumber I want to find corresponding process id. the first two lines below does not show the output ...
9
votes
4answers
5k views

How do I handle switches in a shell script?

Are there some built-in tools that will recognize -x and --xxxx as switches, and not arguments, or do you have to go through all the input variables, test for dashes, and then parse the arguments ...
13
votes
5answers
22k views

Move folder content up one level

I have a directory that is unpacked, but is in a folder. How can I move the contents up one level? I am accessing CentOS via SSH.
45
votes
2answers
3k views

Shebang starting with `//`?

I'm confused about following script (hello.go). //usr/bin/env go run $0 $@ ; exit package main import "fmt" func main() { fmt.Printf("hello, world\n") } It can execute. (on MacOS X 10.9.5) $ ...
3
votes
3answers
98 views

Print a message from the left-hand side of a pipe

I want to use a variable from the main shell in a sub shell. I did: export mysql_root_password="test" ( echo $mysql_root_password ) | dialog --gauge "Working hard..." 6 50 There is no output. ...
1
vote
3answers
559 views

Script to count files matching a pattern in subdirectories

I wrote the following script for finding the number of pdf and tex files from the current directory, including the subdirectories and hidden files. The following code is able to find the number of pdf ...
2
votes
2answers
184 views

Meaning of \ before environment variable

In ksh on an old Solaris box I used: export PS1="$PWD $" to set the prompt to the current directory. It works great until you cd elsewhere, then you see that it's not evaluating PWD each time. I ...
5
votes
3answers
686 views

Why does “$ sudo echo 'yo' ” work even though echo is a shell built in function? [duplicate]

I understand that sudo cd /directory will return: sudo: cd: command not found because cd is a shell builtin and not a binary. But then, why does sudo echo 'this is a test' works fine? What ...
0
votes
1answer
54 views

How can I echo dollar signs?

For example, $PATH and $HOME When i type echo $PATH it returns my $PATH, but i want to echo the word $PATH and not what the actual variable stands for, echo "$PATH" doesn't work either. It might ...
1
vote
3answers
196 views

Brackets in if condition: why am I getting syntax errors without whitespace?

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"] ...
1
vote
2answers
86 views

bash question about if and then [duplicate]

I'm writing this in my script. It has other parts but I'm getting stuck on this part only. if [[$# == $year $month $day ]] ; then cal $day $month $year fi When I run this it give me this msg: ...
1
vote
2answers
146 views

Why are bash tests so picky about whitespace?

As a primarily Java programer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain, why the first one works but not the second and third? #works if ...
1
vote
2answers
44 views

how to use http proxy in terminal

I have to use a http proxy to access Internet. Now I'm using firefox with the http proxy to access Internet. But on terminal, I can' access Internet. So are there ways to use http proxy on terminal ...
9
votes
2answers
2k views

Stay at same working directory when changing to sudo

When working on the command line, I often change to sudo using sudo -i. However, my working directory changes automatically to /root. I never want to go there; I want to stay where I was! How can I ...
2
votes
1answer
4k views

Sort data in descending order of first column, for equal values, use second column in ascending order

Allow me to clarify: Assume I have some keywords with frequency of their usage: 12 Hi 7 Hash 7 C++ 9 Superuser 17 Stackoverflow 9 LaTeX 42 Life 9 Ubuntu What I want, is to sort this data ...
0
votes
1answer
68 views

How do I use vim on the command line to add text to the middle of a file?

I am trying to write a command along the lines of the following: vim -c "XXXXXX" myFile Instead of the "XXXXX" I want to supply some commands to vim to add some text to an arbitrary point in the ...
2
votes
4answers
161 views

How to read certain lines after a find some text?

How i can read a certain number of lines after find some text? Eg.: Read next 2 lines after find "Unix" on: Test 1 Test 2 Test 3 Test 4 UNIX Test 5 Test 6 Test 7 Test 8 Test 9 Result can be: ...
-1
votes
2answers
31 views

Matching multiple patterns with ls

I'd like to get a list of fail logs in the current directory for use in an outside script, the logs are determined with the pattern FAIL in the filename, so I've been using a FAIL* to feed my script ...
8
votes
2answers
16k views

How to check if there are no parameters provided to a command?

How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
5
votes
2answers
718 views

How can I forward a already forwarded ssh key into tmux?

I'm connecting through SSH from a machine where my keys are stored, forwarding to a second machine, and then try to use the keys from within tmux. I appear to "loose" the forwarding when entering ...
13
votes
4answers
2k views

Share aliases and PATH setting between zsh and bash

The shell that I normally use is zsh. I have several aliases to enable color in some programs such as ls and grep. I've also set my custom path so that I can execute programs in non-standard place ...
1
vote
2answers
1k views

Extending history search in zsh

How to extend Ctrl+R search in zsh? It cannot find entry despite it is in history file. Edit: My .zshrc: setopt AUTO_CD setopt CORRECT_ALL setopt EXTENDED_GLOB # History SAVEHIST=10000 ...
1
vote
2answers
55 views

Check that a bash script has exactly two arguments which are directories

I'm writing a bash script that compares two directories, but I'm not sure how to check conditions For example, if the user enters 1 arg instead of 2 (we need 2 since we're comparing two directories), ...
7
votes
3answers
229 views

Empty a file without grep subsequently treating it like a binary file

Currently I have netcat piping output to tee which is writing to output.txt with nc -l -k -p 9100 | tee output.txt I want to monitor this output, so I'm watching it with tail -f | egrep -i 'regex' ...
3
votes
1answer
17 views

Why does unbuffer command affect time command?

I noticed something odd when using the unbuffer command with time command. Here I just want to see how long takes to find a certain file in my system: time find . -name unixstuff output is : ...