Encoding problem with the batch ipconfig command - batch-file

My goal is to export some commands into a txt file and the keep the accent (é, è, î, etc).
I have two different commands :
CHCP 65001
ipconfig /all 1>>file.txt
CHCP 65001 is supposed to change the encoding to UTF-8 (so the accent are supposed to work).
But the second one, ipconfig /all does not work at all (all of the accents are replaced with strange characters) and when I convert the file to UTF-8 within notepad++ it does not work either (there is still strange characters like x93, x85, ...).
What I got (just an extract) :
DHCP activ‚. . . . . . . . . . . . . . : Non
Configuration automatique activ‚e. . . : Oui
Adresse IPv4. . . . . . . . . . . . . .: x.x.x.x(pr‚f‚r‚)
Masque de sous-r‚seau. . . .ÿ. . . . . : x.x.x.x
Passerelle par d‚faut. . . .ÿ. . . . . : x.x.x.x
What I want :
DHCP activé. . . . . . . . . . . . . . : Non
Configuration automatique activée. . . : Oui
Adresse IPv4. . . . . . . . . . . . . .: x.x.x.x(préféré)
Masque de sous-réseau. . . . . . . . . : x.x.x.x
Passerelle par défaut. . . . . . . . . : x.x.x.x
I also tried to export an echo in the txt file before, to use powershell to execute the command (powershell "my command" 1>>file.txt). I also tried to do it from the command line AND if I redirect the command to a txt file and then write type file.txt everything is fine. BUT when I open the file.txt after that, the bugged characters are back T_T. but nothing worked...
How do I export my ipconfig command with the accents ?
Feel free to ask any questions for more informations, I'll try to answer you.
PS : my computer is on french so there is accents litteraly EVERYWHERE
(and sorry for my bad english)
EDIT : I just deleted the example with the <command1> because it was useless and something completely different from my problem so I'm trying with just the 2 commands CHCP and ipconfig.

Related

Array not reading all values

I'm trying to get this arrays to work, but it reads only the first folder.
SFA=(folder1 folder2)
and the code:
for folders in "${SFA[#]}"
do
echo /media/$SFA
done
But it only seems to iterate trough one folder (tv1) 2 times. I can't see anything wrong with the code...
You can do something like this:
declare SFA=(folder1 folder2)
for folders in "${SFA[#]}"; do echo /media/$folders; done
The Output will be as follows:
:~$
/media/folder1
/media/folder2
I found my own mistake...
The code looks like this now and works as intended.
FOLDERARRAY=(tv1 tv2)
### Check for FOLDERS in FOLDERARRAY
#
for FOLDERS in "${FOLDERARRAY[#]}"
do
# echo /media/$SFA
if [ ! -d /media/$FOLDERS ]; then
echo /media/$FOLDERS; status="Couldn't find /media/$FOLDERS"
# exit 0
else
echo "found folder: /media/$FOLDERS"
find /media/${FOLDERARRAY} -mtime -7 -type f -size +2048 -exec
basename {} \;| sort |uniq >> /tmp/v$DATE-weekly.txt;
fi

Accessing Variable effectively from shell script

I have one ini configuration file, I need to create a shell script using this configuration. What is the easiest method to access all variable, Can be used effectively from the shell script.
Can I use an array or something? Now planing to find the count of [] brackets then through awk get all variables one by one. Please suggest if any easiest way to effectively
cat app.ini
Below the output of my sample configuration file. Can be N no of Blocks.
[APP1]
name=Application1
StatusScript=/home/status_APP1.sh
startScript=/home/start_APP1.sh
stopScript=/home/stop_APP1.sh
restartScript=/home/restart.APP1.sh
logdir=/log/APP1/
[APP2]
name=Application2
StatusScript=/home/status_APP2.sh
startScript=/home/start_APP2.sh
stopScript=/home/stop_APP2.sh
restartScript=/home/restart.APP2.sh
logdir=/log/APP2/
.
.
.
.
.
[APPN]
name=ApplicationN
StatusScript=/home/status_APPN.sh
startScript=/home/start_APPN.sh
stopScript=/home/stop_APPN.sh
restartScript=/home/restart.APPN.sh
logdir=/log/APPN
/
Consider using a library like bash-ini-parser https://github.com/albfan/bash-ini-parser. It covers a lot of nuances like indentation, whitespaces, comments etc.
The example for your case may look like this:
#!/bin/bash
. bash-ini-parser
cfg_parser app.ini
cfg_section_APP1
echo $name
cfg_section_APP2
echo $logdir
cfg_section_APPN
echo $logdir
Below line help us to locate particular values in each section.
sed -nr "/^\[APP1\]/ { :l /^name[ ]*=/ { s/.*=[ ]*//; p; q;}; n; b l;}" app.ini

use Gnuplot to many files at once

I have many csv files that should be displayed each one in many graphs :
data1.csv
data2.csv
data3.csv
data4.csv
data5.csv
.
.
.
dataN.csv
I am using a script to display only one chart for one file :
./script.sh data1.csv
How to generate graphs for all files at once ?
Call your script inside a loop
for file in $file_list; do
./script.sh $file
done
or, if you want all plots in one file then put this loop inside plot script (gnuplot 4.4+)
plot for [i=1:N] 'data'.i.'.csv' . . .
EDIT: If you want to pass filenames through command line args, wrap it in another script like this
#!/bin/bash
for file in "$#"; do
[ -f $file ] && ./script.sh $file
done

How to distinguish between files and directories using ssh2 on a remote server

We are attempting to move only the files from a remote server and putting the files directories into our database, so we need to be able to distinguish between a file and a directory. We have successfully been able to connect via SSH2 and we are able to read and display the files and directories within the remote path top directory. However, we have not been able to locate a php script that will allow us to find whether the returned name is a directory or a file. Below are a few examples we have tried. Any help is greatly appreciated and we thank you in advance.
$connection = ssh2_connect('www.myremote.com', 22);
ssh2_auth_password($connection, 'user', 'pw');
$sftp = ssh2_sftp($connection);
// THIS WORKS NICELY TO DISPLAY NAME FROM REMOTE SERVER
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "Directory handle: $handle<br>";
echo "Entries:<br>";
while (false != ($entry = readdir($handle))){
echo "$entry<br>";
}
// ONE OPTION THAT DOES NOT WORK
$handle = opendir("ssh2.sftp://$sftp/remotepath/");
echo "<br><br>2Directory handle: $handle<br>";
echo "4Entries:<br>";
while (false != ($entry = readdir($handle))){
if (is_dir(readdir($handel.''.$entry) ) ) {echo "<strong>$entry</strong><br>"; } else {
echo "$entry<br>"; //}
}
// ANOTHER OPTION THAT RETURNS AN EMPTY RESULT
$files = scandir('ssh2.sftp://'.$sftp.'/remotepath/');
foreach ($files as $file):
if (is_dir('ssh2.sftp://'.$sftp.'/remotepath/'.$file) ) {"<strong>".$file."</strong><br>"; } else { $file.'<br>'; }
endforeach;
if you use the Linux command line find this will help:
this is how you find only files named blah:
find . -type f -name *blah*
this is how you find only directories named blah:
find . -type d -name *blah*
in this case you could to the following to find all files in the /tmp directory (without going into the subdirectories of /tmp (maxdepth 1)) named anything:
$connection->exec('find /tmp -maxdepth 1 -type f -name "*"');
EDIT:
Ok, so here's a bit more code. This will connect to a server and echo a list of all directories in your home directory and then all files in your home directory:
$connection = ssh2_connect($host, 22);
ssh2_auth_password($connection, $user, $pass);
$the_stream = ssh2_exec($connection, '/usr/bin/find ~ -maxdepth 1 -type d');
stream_set_blocking($the_stream, true);
$the_result = stream_get_contents($the_stream);
echo "Directories only: <br><pre>" . $the_result . "</pre>";
fclose($the_stream);
$the_stream = ssh2_exec($connection, '/usr/bin/find ~ -maxdepth 1 -type f');
stream_set_blocking($the_stream, true);
$the_result = stream_get_contents($the_stream);
echo "Files only: <br><pre>" . $the_result . "</pre>";
fclose($the_stream);
You should be able to parse $the_result into an array by splitting on newlines or some such and get just the files or just the directories. Remove the "-maxdepth 1" from the find and you'll recurse through all subdirectories.

Looping through sub folders not working Unix

I have a folder with multiple sub-folders and each sub-folder contains 10-15 files. I want to perform a certain operation only on the text files in these folders. The folders contain other types of files as well. For now, I am just trying to write a simple for loop to access every file.
for /r in *.txt; do "need to perform this on every file"; done
This gives me an error -bash: ``/R': not a valid identifier
Thanks for the help.
P.S I am using cygwin on Win 7.
Your /r is the problem, that's not a valid identifier (as bash said, you need to drop the /). Also, this won't recurse into subdirectories. If your operation is simple, you can directly use the exec option of find. {} is a placeholder for the filename.
find . -name "*.txt" -exec ls -l {} \;
Otherwise, try something like
for r in $( find . -name "*.txt" ) ; do
echo $r
#more actions...
done
With bash:
shopt -s globstar
for file in **/*.txt; do ...
I would use "find" for your application case
Something like
find . -name "*.txt" -exec doSomeThing {} \;

Resources