Tagged Questions
2
votes
4answers
105 views
How to catch an error in a linux bash script?
I made the following script:
# !/bin/bash
# OUTPUT-COLORING
red='\e[0;31m'
green='\e[0;32m'
NC='\e[0m' # No Color
# FUNCTIONS
# directoryExists - Does the directory exist?
function directoryExists ...
13
votes
4answers
820 views
Why does while [ 0 ] go into infinite loop?
I see the same behaviour for below loop as the loop with while [ 1 ]. Why is that so?
while [ 0 ]; do
echo "hello"
done
3
votes
2answers
64 views
How do I check if a file is a symbolic link to a directory?
I can check, if a file exists and is a symbolic link with -L
for file in *; do
if [[ -L "$file" ]]; then echo "$file is a symlink"; else echo "$file is not a symlink"; fi
done
and if it is a ...
9
votes
4answers
310 views
Search for a previous command with the same prefix when I press Up at a shell prompt
Coming from a FreeBSD world I wish to make the Linux terminal behave like FreeBSD one, especially the 9.1 version, basically when you type cd in the terminal and push the "up" arrow you can browse all ...
5
votes
2answers
106 views
How to stop Fork Bomb out of memory error - RHEL 6
I set up test VM to test the effect of fork bombs. So I edited the limits.conf as follows for root user:
root hard nproc 512
Now I drop a fork bomb like so:
:(){ :|:& };:
After ...
1
vote
2answers
36 views
How to find certain files and move them to a new directory in the same command?
So I was looking around and all I have found is how to do this only if the directory is already made, which is this:
find source -name '*.z' -exec cp {} destination \;
But how can I make a new ...
4
votes
1answer
70 views
How to echo variables using cat into file?
I have simple script like this:
#!/bin/bash
BOO=bla-bla-bla
cat > ./Makefile <<'EOF'
Hello
INCLUDES := -I/data/opt/include/ -Ideps/ -I$BOO/include
EOF
Yet it outputs into ...
2
votes
2answers
66 views
Bash doesn't read .bashrc unless manually started
bash won't source .bashrc from an interactive terminal unless I manually run bash from a terminal:
$ bash
or manually source it:
$ source ./.bashrc
or running:
$ st -e bash
Here's some useful ...
1
vote
1answer
44 views
using find in Bash Script and checking if file is present
var= "$(find . -name 'gen*.bt2')"
if [ "$var" == "" ]
then
echo bad
else
echo great
fi
I get errors
./script.sh: line 4: ./gen.4.bt2
./gen.rev.1.bt2
...
2
votes
1answer
39 views
Any better method than this for sorting files by their creation date?
I needed to display a list of directories sorted by their creation date and I came up with this code snippet that I thought was kind of clever. Is there a more obvious way to do this that I'm missing?
...
-2
votes
1answer
57 views
Less-than sign after done in a shell script
Should be easy but want to know why the “left arrow” < is used instead of right arrow.
if
fi
done < a.txt
What does it mean by left arrow?
I thought since I am done with the ...
0
votes
2answers
63 views
Shell, bash, linux : after variable declaration, why semi-colon and what does it do?
Here is the line.
variable=`ls -A $1 | grep '[abc]'; ls -1 $1`
I understand the line before the semi-colon but I do not understand how this line works after semi-colon. Does it
after the ...
-1
votes
1answer
53 views
$1 means for … ? without any input?
I have one program that starts with this line. What does this mean? I am having trouble googling because of the dollar sign.
How come $1 without any parameters? And what does it mean by -d here?
...
2
votes
4answers
81 views
how to convert number to time format in shell script?
I want to cut a video into about 10 minute parts like this.
ffmpeg -i video.mp4 -ss 00:00:00 -t 00:10:00 -c copy 01.mp4
ffmpeg -i video.mp4 -ss 00:10:00 -t 00:10:00 -c copy 02.mp4
ffmpeg -i video.mp4 ...
2
votes
2answers
84 views
BASH and Subshells
From what I gather from the online docs, the following should spawn a subshell for the part of the command embedded with {}:
$ bash -c '{ sleep 10; echo "Sleeping process", $$; } & echo $$; '
...
1
vote
1answer
67 views
What is the use of ; in a single line command?
What does ; mean in single line scripts like this:
while true; do sudo -n true; sleep 60; kill -0 '$$' || exit; done 2>/dev/null &
Does it mean new line, or "next command"?
0
votes
1answer
50 views
LD_LIBRARY_PATH not found - CentOS
I am trying to run the following two commands...
LD_LIBRARY_PATH =/opt/oracle/instantclient_12_1
export
LD_LIBRARY_PATH
But when I run the first command I get an error
-bash: ...
0
votes
0answers
41 views
How to echo to running process in shell? [migrated]
If I have a C++ program that expects input to cin from the console during run-time, how can I automate this input from a shell script? I also need to store its output in a file but that part is easy. ...
0
votes
1answer
52 views
List of special characters in the shell [closed]
I am not sure what they are called, but where could I find a list of wild cards?
I see things like:
$f
%s
$1
Is there a definitive list for what each one represents?
1
vote
1answer
54 views
Is there a simple way of limiting the number of child processes that execute in parallel?
I have a bash script that kicks off multiple rsync processes in parallel. I'd like to limit the number of simultaneous child processes but not serialize the whole script.
Here's a simplified version ...
2
votes
3answers
65 views
cron: bash syntax not working
I am trying to add following command to crontab:
I=1; for X in $(/bin/ls -r /var/tmp/*); do [ $((I++)) -le 28 ] && echo "lower" || echo "higher"; done
When executed on the command line (in ...
1
vote
0answers
36 views
What's the difference between `` and $()? [duplicate]
When you want to return a command line output, you may consider using two methods: `cat file` or $(cat file). As far as I know, they are equivalent before my eyes.
Is there a difference between both? ...
7
votes
3answers
226 views
which shell will sudo use to execute a shell script without the shebang line
My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1.
First I login as a normal user:
$ whoami
fin
$ cat /etc/passwd | grep -i "root\|fin"
root:x:0:0:root:/root:/bin/bash
...
0
votes
2answers
99 views
Shell script - syntax error near unexpected token `['
When I open my terminal I get the following error:
bash: /home/ai/.bashrc: line 75: syntax error near unexpected token `['
bash: /home/ai/.bashrc: line 75: `if [ -x /usr/bin/dircolors ]; then'
The ...
0
votes
0answers
40 views
Do all Debian-based distributions come with the same default .bashrc?
I ask this out of curiosity because while I have kept my system configuration files under version control for some time now, they've only ever been for OS X. Recently, though, I've been developing ...
11
votes
2answers
350 views
What is the name of the shell feature `>(tee copyError.txt >&2)`?
I need to log stdout and stderr to logfiles, but only show the error messages on screen. I can do this with:
cp -rpv a/* b 1> copyLog.txt 2> >(tee copyError.txt >&2)
Which I found ...
32
votes
4answers
968 views
What exactly is an environment variable?
I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but ...
1
vote
1answer
49 views
bash: export: `--wait': not a valid identifier
When I run git-sh from my terminal it works fine. But I get the following warning.
bash: export: `--wait': not a valid identifier
What is this and how do I get rid of it?
4
votes
3answers
401 views
Summing up an array inside of awk?
I have the following piece of code:
sum1=
sum2=
declare -a a
echo $temp | awk '{split($0,a,","); name=a[1] ; for(i=2;i<=4;i++) sum1+=a[i] ; for(i=5;i<=7;i++) sum2+=a[i] }'
This code is ...
0
votes
2answers
116 views
Monitor servers
I want to monitor three server pages. These three pages contain a simple text (for example it says it's "working fine"). I want to be notified when anything other than this message is displayed, e.g. ...
0
votes
3answers
73 views
Run two commands on same argument
I was wanting to maybe alias a command such that the command ran as per usual but also did something else on the argument/s before it was executed. An example might be:
alias ls="echo displaying ...
1
vote
2answers
40 views
What happens to argument in alias after using it once? [duplicate]
Why does this only print arg out once?
$ alias test="echo $1 $1 $1"
$ test arg
arg
This is on GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu).
5
votes
2answers
95 views
If I type “sudo” at the beginning of a one liner, does it apply to the rest of the commands?
If I type sudo at the beginning of a one liner in bash, does it apply to the rest of the commands?
In other words, is this:
sudo foo | foo2 | foo3
equivalent to this:
sudo foo | sudo foo2 | ...
1
vote
1answer
51 views
Find where a bash command is defined or sourced from? [duplicate]
Sometimes I want to know what a command I type into bash actually evaluates to. Usually I can figure out the location of the executable very easily with which.
$ which vim
/usr/bin/vim
But what if ...
2
votes
1answer
43 views
usage of ! -d in a if condition
When reading the bash script for hadoop management, I found the following one
if [ ! -d "${HADOOP_HOME}" ]; then
if [ -d "${CDH_HADOOP_HOME}" ]; then
what does ! -d "${HADOOP_HOME}" and -d ...
20
votes
9answers
624 views
What's the most resource efficient way to count how many files are in a directory?
CentOS 5.9
I came across an issue the other day where a directory had a lot of files. To count it, I ran ls -l /foo/foo2/ | wc -l
Turns out that there were over 1 million files in a single ...
2
votes
1answer
62 views
Screen - inherit current window's environment
In GNU Screen, how can I create a new window whose shell inherits the environment of the shell in the current window?
The usual Ctrl+A C doesn't seem to do this.
2
votes
1answer
52 views
bash - inherit directory stack in children
If, in my newly created bash, I do:
pushd foo
pushd bar
dirs -l -v
I will get something like
0 bar
1 foo
2 old_dir
If I then do
bash
dirs -l -v
I get only
0 bar
The same thing happens ...
2
votes
2answers
111 views
Discrepancy between the two expansions
If you input the command ls * and echo *, you will notice the remarkable discrepancy between the results. The former recursively includes while the latter totally excludes files in subdirectories. ...
1
vote
1answer
146 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
...
-3
votes
1answer
63 views
Chaining && and || in the shell [closed]
Why does this not work as expected?
$ false && { echo ok; echo ok; } && { echo notOK; }
and this not:
$ false && { echo ok; echo ok; } || { echo notOK; }
I don't ...
13
votes
3answers
464 views
Bash: logical operators precedence &&, ||
I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything.
true || echo aaa && echo ...
14
votes
4answers
572 views
What happens if you edit a script during execution?
I have a general question, which might be a result of misunderstanding of how processes are handled in Linux.
For my purposes I am going to define a 'script' as a snippet of bash code saved to a text ...
2
votes
2answers
136 views
Color scheme is lost on shell change
My default login shell is ksh which shows my default color scheme which i set via putty.
But when i change my shell to bash my color scheme is lost.
Is there a way to retain the color scheme?
EDIT:
...
3
votes
4answers
119 views
Capture output of a bash command, parse it and store into different bash variables
Explanation:
I have a small bash script which simply runs any Linux command (e.g. say ifconfig)
The typical output of ifconfig is something like this:
eth0 Link encap:Ethernet HWaddr ...
4
votes
2answers
341 views
Why doesn't my ~/.bash_profile work?
I'm using Linux Mint. My login shell (cat /etc/passwd | grep myUserName) is bash.
After I start my graphical desktop environment and run terminal emulator from it, I could see that .bash_profile is ...
2
votes
2answers
53 views
Filtering the result of the find command, so that it returns only directories
Is it possible to get only the results from find that are directory paths? Using find with some option, or using grep or some other utility the results are piped into as a filter?
I thought something ...
2
votes
2answers
76 views
Writing scripts that use different commands in different shells [duplicate]
How can I write a script that is shell independent? I want to be able to run one script which will run in ksh, sh and bash.
For example: If I have a line in my script like whence ls which will run in ...
2
votes
3answers
87 views
Simplified navigation in terminal
I know IDEs are the wave of the future, but I always find myself coding in vim in a Linux terminal. Old dog. New tricks.
Anyway, navigation becomes challenging when dealing with long package names. ...
5
votes
1answer
140 views
How can I execute local script on remote machine and include arguments?
I have written a script that runs fine when executed locally:
./sysMole -time Aug 18 18
The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script.
Now, this script is ...