Linked Questions

2 votes
1 answer
6k views

How to use negation of a command in a while loop with grep in shell script?

Is there any way to use while loop and grep all together? See my example: while [[ !(grep -R -h "${text}" ${path}) ]]; do ... done It says: ./test_script.sh: line 1: conditional binary ...
  • 145
1 vote
2 answers
3k views

When to quote a parameter expansion, and enclose parameter name in braces? [duplicate]

In a parameter expansion: Is it always better (or not worse) to double quote a parameter expansion than not? Are there cases where double quoting is not suggested? When is it necessary to add braces ...
  • 94.9k
1 vote
4 answers
2k views

Rename file using grep and for

I would like to rename files with following extensions: .txt, .data, .conf to ".xml" hello.txt -> hello.xml To do so, file must also contain the following line: <?xml version="1.0" encoding="UTF-...
2 votes
2 answers
2k views

Bash: Globbing and file conditions [duplicate]

I am trying to check wether a file exists or not: This works as expected: if [ -e "/tmp/test.*.lock" ]; then echo "exists!" fi This does not: if [[ -e /tmp/test.*.lock ]]; then echo "exists!" ...
  • 68
2 votes
2 answers
361 views

Why is it not required to double quote `$bar` in assignment `foo=$bar`? [duplicate]

From https://unix.stackexchange.com/a/32227/674 The main places where it's safe not to use the double quotes are: in an assignment: foo=$bar (but note that you do need the double quotes in ...
  • 94.9k
2 votes
1 answer
747 views

Conditional emoji on terminal success and failure

I am using a Mac and I want to be able to show emoji X for every successful command that I type in and emoji Y for every command that results in failure.
  • 131
1 vote
2 answers
3k views

downloading a file behind a link

I want to write a script to download the WEbStorm IDE from Jetbrains and then do further stuffs. But I am unable to download the tar ball. I am following this, but it doesnt solve my problem. ...
3 votes
1 answer
431 views

how to add a custom option to the mkdir command

I have created lots of directories and I would to make my life lazy and to auto cd into the directory that I made with the option of -g with the result of mkdir -g foo The terminal would be like this:...
0 votes
2 answers
2k views

How to ssh to a remote host using from within a bash script function? [duplicate]

I'm new to bash scripting. I want to call a function from my script which will ssh into a remote computer (on my LAN) and run a command. So far I have: function run_ssh_command { target_ip=$1 ...
  • 533
2 votes
1 answer
705 views

If condition works for first time but not for the second time

I have defined the below for and if condition in a function. For the first time it runs well. Second time it doesn't gets executed after the for loop, it just writes the echo statement. Why does it ...
1 vote
4 answers
567 views

How to use as argument the characters + , - , x , / in a script [duplicate]

I am trying to write a script I will call calc.sh that does basic calculations using the arguments for the script ( $1, $2 and $3). For instance I'd like to get 2 as result for ./calc.sh 10 / 5 My ...
1 vote
1 answer
939 views

Populate array with a sed/echo combination

I'm trying to rename multiple files containing dates. I want to split the date into year, month and day and then create a new filename in a directory tree like year/month-year/filename_yearmonthday....
0 votes
1 answer
555 views

how to nest conditional script operators -a -o in an if statement in bash

If i want to combine the -a and -o script operators in an if statement, how could I do that? For example: if [ ( -e file.txt -a -r file.txt ) -o ( -e file2.txt -a -r file2.txt ) ]; then .... fi Can I ...
  • 11
-1 votes
2 answers
83 views

if condition throws error

In bash coding, line3 is a path taken from xyz/symlinks_paths.txt. while read -r line3 do if [[ $(ls -lh $line3 | grep zzz.exe | grep '[8-9][0-9][0-9][MG]') -ne 0 ]] then echo $...
  • 3
-2 votes
2 answers
132 views

How to use globs in flow control?

I have a list of directories (or folders if you will). In each directory is a file called <something>.txt that is generated from a database dump. I want to run a cron job that goes through and ...

15 30 50 per page