Linux batch: create multi folder by combine name with serial number [duplicate] - linux

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I'm try to create multi folder by combine string and counter. I don't why what is the wrong with my code:
echo 'Start'
let count=0
for p in {1..10}
do
DirName= "dir"
NUM = "${DirName}${count}"
let count++
mkdir $NUM
mkdir "$NUM"/decoded
done
I got this kind of error
./test.sh: line 6: dir: command not found
./test.sh: line 7: NUM: command not found
thank in advance

No need to use a loop here. The shell will do all the necessary expansion for you. In fact, you're already relying on the shell to expand {1..10} for you as part of your for loop. So you can just use that expansion directly with mkdir. Also by using mkdir -p <path> (make parent directories as needed), you can avoid having to first do mkdir $NUM before doing mkdir $NUM/decoded.
Putting it all together, you can do what you need in a single line:
mkdir -p dir{1..10}/decoded
Edit: To answer your question more directly regarding the command not found errors, it looks like (as Bjorn A. mentioned) you just need to get rid of the spaces before and after the = in your variable assignments.

You cannot have spaces around the assignment operator in bash. Lines 6 and 7 must look like:
DirName="dir"
NUM="${DirName}${count}"

Related

Bash substring extraction purpose in "ls -lashtg ${1:-.}" [duplicate]

This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 1 year ago.
I ran across the following Bash function which was suggested as a useful alias to add to the .bashrc file. It lists the last 13 files in a directory that were modified.
I don't understand what is being done with the ${1:-.} argument, though. It looks like some kind of substring extraction, but I couldn't find the meaning of -. in the Advanced Bash Scripting Manual.
I tried the command in a few directories and didn't notice any difference between the output when I removed this argument. My guess is that it's there to prevent an error when encountering some specific type of file or file name. What is it doing? And what is the purpose of including it in the command?
function lst()
{
ls -lashtg ${1:-.} | head -13
}
$1 is the first command line argument. Im sure you know.
${1:-.} simply puts a . when no first line argument is given.
Thus
lst
Translates to
ls -lashtg . | head -13
It would workout without the substitution I guess. But I also guess that this is just there as a best practice

Bashscript throws command error when populating variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Bash variable from command with pipes, quotes, etc
(2 answers)
Variable variable assignment error -"command not found"
(1 answer)
Closed 1 year ago.
i have the following two lines in a batch script
iperf_options=" -O 10 -V -i 10 --get-server-output -P " $streams
$iperf_options=$iperf_options $proto
and
$streams = 2
$proto = -u
but when i run this i get the following error.
./bandwidth: line 116: -O: command not found
I am simply trying to wrote a string and then append it to a variable so why does it throw the error on the -O?
I have looked about the web but i jsut seem to find stuff about spaces around the "="
any help greatfully recived.
Thankyou
code block to show error
proto=-u
streams=2
iperf_options=" -O 10 -V -i 10 --get-server-output -P " $streams
$iperf_options=$iperf_options $proto
running this will give this out put
./test
./test: line 3: 2: command not found
./test: line 4: =: command not found
There are two main mistakes here, in a variety of combinations.
Use $ to get the value of a variable, never when setting the variable (or changing its properties):
$var=value # Bad
var=value # Good
var=$othervar # Also good
Spaces are critical delimiters in shell syntax; adding (or removing) them can change the meaning of a command in unexpected ways:
var = value # Runs `var` as a command, passing "=" and "value" as arguments
var=val1 val2 # Runs `val2` as a command, with var=val1 set in its environment
var="val1 val2" # Sets `var1` to `val1 val2`
So, in this command:
iperf_options=" -O 10 -V -i 10 --get-server-output -P " $streams
The space between iperf_options="..." and $streams means that it'll expand $streams and try to run it as a command (with iperf_options set in its environment). You want something like:
iperf_options=" -O 10 -V -i 10 --get-server-output -P $streams"
Here, since $streams is part of the double-quoted string, it'll be expanded (variable expand inside double-quotes, but not in single-quoted), and its value included in the value assigned to iperf_options.
There's actually a third mistake (or at least dubious scripting practice): building lists of options as simple string variables. This works in simple cases, but fails when things get complex. If you're using a shell that supports arrays (e.g. bash, ksh, zsh, etc, but not dash), it's better to use those instead, and store each option/argument as a separate array element, and then expand the array with "${arrayname[#]}" to get all of the elements out intact (yes, all those quotes, braces, brackets, etc are actually needed).
proto="-u" # If this'll always have exactly one value, plain string is ok
streams=2 # Same here
iperf_options=(-O 10 -V -i 10 --get-server-output -P "$streams")
iperf_options=("${iperf_options[#]}" "$proto")
# ...
iperf "${iperf_options[#]}"
Finally, I recommend shellcheck.net to sanity-check your scripts for common mistakes. A warning, though: it won't catch all errors, since it doesn't know your intent. For instance, if it sees var=val1 val2 it'll assume you meant to run val2 as a command and won't flag it as a mistake.

Bash script file as input [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
i am trying to give an file as input to me my shell script:
#!/bin/bash
file ="$1"
externalprogram "$file"
echo 'unixcommand file '
i am trying to give the path to my file but it says always
cannot open `=/home/username/documents/file' (No such file or directory)
my path is this /home/username/Documents/file
i do this in terminal : ./myscript.sh /home/username/Documents/file
can someone help me please?
When you say
file ="$1"
with a space after "file", you're running something called file with =$1 as an argument. There probably actually is a utility called file. If you want to assign $1 to a variable called file, you don't need the space:
file="$1"
there shouldn't be a space before = in the second line.
file=$1 should be good enough.
Check what shellcheck says about your code:
^-- SC1068: Don't put spaces around the = in assignments (or
quote to make it literal).
You can read more about SC1068 case on its Github
page.
#!/bin/bash
file=$1
code $file
echo "aberto o arquivo ${file} no vscode"
I made this code snippet to demonstrate, I pass a path and it opens the file in vscode

Change value of variable insead loop Bash [duplicate]

This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.

Adding one or two directories to PATH variable in linux ( Using Bash script) [duplicate]

This question already has answers here:
Check number of arguments passed to a Bash script
(10 answers)
Add a bash script to path
(5 answers)
Closed 5 years ago.
So far
I have the following code:
#!/bin/bash
echo "Adding new path...."
if [[$# -eq1] || [$# -eq2]]
then
if [$# -eq2]
then
export PATH=$PATH:/$1:/$2
fi
if [$# -eq1]
then
export PATH=$PATH:/$1
fi
else echo "Incorrect number of parameters. No more than two directories can be added at once."
fi
echo $PATH
exit 0
When I run this script passing it one parameter i get an error:
"./addDir: line 3: [[1: command not found
./addDir: line 3: [1: command not found "
when I run it with 2 parameters instead of "1" it says "2"
What's going on?
You're missing some spaces. Basically, if you're trying to use the [...] construction, you need to have spaces before and after each bracket - think of [ as being the name of a command, in the same way as echo, and ] as being an argument to that command. (In fact, there might actually be a /bin/[ program on your system.) Just as you can't type echofoo and expect it to run the echo program, similarly you can't type [[$# if you expect it to run [.
In your case, you'd need to do things like
if [ $# -eq 2 ]; ...
And for the compound test you're doing in line 3, I don't think you can use [ and ] within the test. In other words, don't use those brackets for grouping; it has to be [ something ] where the something doesn't contain any brackets. Read the relevant section of the bash man page for the full details of what you can put there.
There is also a shell construct [[ ... ]] which does basically the same thing but has different syntax. You could use that instead, but be aware that it's very different from [ ... ].

Resources